|
| 1 | +package.path = "./src/?.lua;./src/?/init.lua;./spec/?.lua;./spec/?/init.lua;" .. package.path |
| 2 | + |
| 3 | +local mock_ngx = require("helpers.mock_ngx") |
| 4 | +local gherkin = require("helpers.gherkin") |
| 5 | + |
| 6 | +-- --------------------------------------------------------------------------- |
| 7 | +-- Call tracking (reset per scenario) |
| 8 | +-- --------------------------------------------------------------------------- |
| 9 | +local _calls = {} |
| 10 | + |
| 11 | +-- --------------------------------------------------------------------------- |
| 12 | +-- Lightweight mock — mimics wrapper.get_provider prefix logic without |
| 13 | +-- loading the real module (which pulls in ngx at module init time). |
| 14 | +-- --------------------------------------------------------------------------- |
| 15 | +local _PROVIDER_PREFIXES = { |
| 16 | + "/gemini-compat", "/openai", "/anthropic", "/gemini", |
| 17 | + "/grok", "/groq", "/mistral", "/deepseek", "/perplexity", |
| 18 | + "/together", "/fireworks", "/cerebras", "/ollama", |
| 19 | +} |
| 20 | + |
| 21 | +local _wrapper_mock = { |
| 22 | + get_provider = function(path) |
| 23 | + if type(path) ~= "string" then return nil end |
| 24 | + for _, prefix in ipairs(_PROVIDER_PREFIXES) do |
| 25 | + if path:sub(1, #prefix) == prefix then |
| 26 | + local next_char = path:sub(#prefix + 1, #prefix + 1) |
| 27 | + if next_char == "/" or next_char == "" then |
| 28 | + return { prefix = prefix } |
| 29 | + end |
| 30 | + end |
| 31 | + end |
| 32 | + return nil |
| 33 | + end, |
| 34 | + access_handler = function() |
| 35 | + _calls.wrapper = (_calls.wrapper or 0) + 1 |
| 36 | + end, |
| 37 | +} |
| 38 | + |
| 39 | +local _decision_mock = { |
| 40 | + access_handler = function() |
| 41 | + _calls.decision_api = (_calls.decision_api or 0) + 1 |
| 42 | + end, |
| 43 | +} |
| 44 | + |
| 45 | +-- Pre-populate package.loaded so access.lua picks up mocks via require() |
| 46 | +package.loaded["fairvisor.wrapper"] = _wrapper_mock |
| 47 | +package.loaded["fairvisor.decision_api"] = _decision_mock |
| 48 | + |
| 49 | +-- --------------------------------------------------------------------------- |
| 50 | +-- Step definitions |
| 51 | +-- --------------------------------------------------------------------------- |
| 52 | +local runner = gherkin.new({ describe = describe, context = context, it = it }) |
| 53 | + |
| 54 | +runner:given("^nginx mode is \"(.-)\" and uri is \"(.-)\"$", function(ctx, mode, uri) |
| 55 | + mock_ngx.setup_ngx() |
| 56 | + ngx.var.fairvisor_mode = mode |
| 57 | + ngx.var.uri = uri |
| 58 | + _calls = {} |
| 59 | +end) |
| 60 | + |
| 61 | +runner:when("^the access dispatcher runs$", function(ctx) |
| 62 | + local chunk, err = loadfile("src/nginx/access.lua") |
| 63 | + assert.is_not_nil(chunk, "loadfile(src/nginx/access.lua) failed: " .. tostring(err)) |
| 64 | + chunk() |
| 65 | +end) |
| 66 | + |
| 67 | +runner:then_("^wrapper access_handler is called$", function(ctx) |
| 68 | + assert.is_truthy((_calls.wrapper or 0) > 0, |
| 69 | + "expected wrapper.access_handler() to be called but it was not") |
| 70 | +end) |
| 71 | + |
| 72 | +runner:then_("^decision_api access_handler is called$", function(ctx) |
| 73 | + assert.is_truthy((_calls.decision_api or 0) > 0, |
| 74 | + "expected decision_api.access_handler() to be called but it was not") |
| 75 | +end) |
| 76 | + |
| 77 | +runner:then_("^neither handler is called$", function(ctx) |
| 78 | + assert.equals(0, _calls.wrapper or 0, |
| 79 | + "wrapper.access_handler should not be called") |
| 80 | + assert.equals(0, _calls.decision_api or 0, |
| 81 | + "decision_api.access_handler should not be called") |
| 82 | +end) |
| 83 | + |
| 84 | +-- --------------------------------------------------------------------------- |
| 85 | +-- Run scenarios from feature file |
| 86 | +-- --------------------------------------------------------------------------- |
| 87 | +runner:feature_file_relative("features/access.feature") |
0 commit comments