From 1e5874c046c7bee5133f18b6af3eecda93b0b8cf Mon Sep 17 00:00:00 2001 From: dimiro1 Date: Thu, 4 Jun 2026 08:49:10 +0200 Subject: [PATCH 1/3] test: rewrite e2e suite as Cucumber (godog) browser tests Replace the old chromedp/HTTP e2e tests with a Cucumber/Gherkin suite run via godog. Every scenario drives the real dashboard in headless Chrome; function invocation uses the public /fn HTTP endpoint. Feature files stay product-level (no selectors); the step code holds all interaction detail. Also wire the AI/email request trackers in the e2e test server so opening an execution's detail page no longer nil-panics, add an E2E_HEADED flag plus a test-e2e-headed task to watch a real browser, raise the e2e test timeout, and update the README and testing-strategy ADR. --- .github/workflows/test.yml | 5 + README.md | 28 +- docs/adr/0011-testing-strategy.md | 19 +- e2e/features/connected_clients.feature | 9 + e2e/features/execution_history.feature | 28 + e2e/features/invoking_functions.feature | 66 ++ e2e/features/managing_functions.feature | 39 + e2e/features/sign_in.feature | 16 + e2e/features/stored_data.feature | 31 + e2e/features/test_page.feature | 13 + e2e/features/versions.feature | 40 + e2e/function_create_test.go | 125 --- e2e/function_execution_test.go | 322 -------- e2e/function_pages_test.go | 107 --- e2e/functions_list_test.go | 48 -- e2e/godog_test.go | 88 ++ e2e/helpers_test.go | 399 --------- e2e/login_test.go | 53 -- e2e/steps_test.go | 1006 +++++++++++++++++++++++ e2e/world_test.go | 344 ++++++++ go.mod | 10 +- go.sum | 37 + mise.toml | 13 +- 23 files changed, 1777 insertions(+), 1069 deletions(-) create mode 100644 e2e/features/connected_clients.feature create mode 100644 e2e/features/execution_history.feature create mode 100644 e2e/features/invoking_functions.feature create mode 100644 e2e/features/managing_functions.feature create mode 100644 e2e/features/sign_in.feature create mode 100644 e2e/features/stored_data.feature create mode 100644 e2e/features/test_page.feature create mode 100644 e2e/features/versions.feature delete mode 100644 e2e/function_create_test.go delete mode 100644 e2e/function_execution_test.go delete mode 100644 e2e/function_pages_test.go delete mode 100644 e2e/functions_list_test.go create mode 100644 e2e/godog_test.go delete mode 100644 e2e/helpers_test.go delete mode 100644 e2e/login_test.go create mode 100644 e2e/steps_test.go create mode 100644 e2e/world_test.go diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index e9a2bcf..aef6842 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -88,6 +88,11 @@ jobs: e2e: name: E2E Tests runs-on: ubuntu-latest + # The browser suite is slower than the unit tests; give it a clear ceiling + # that sits above the `go test -timeout 20m` budget in `mise run test-e2e`, + # so a genuine hang is reported by go test (with a stack dump) before the job + # is force-killed. + timeout-minutes: 25 steps: - name: Checkout code diff --git a/README.md b/README.md index 824878f..034368a 100644 --- a/README.md +++ b/README.md @@ -450,21 +450,37 @@ This starts a local Go server and opens the test runner at `http://localhost:888 - Route URL generators - UI components (Button, Badge, Table, Pagination, ...) -### E2E Tests (chromedp) +### E2E Tests (Cucumber + chromedp) -End-to-end tests use [chromedp](https://github.com/chromedp/chromedp) to run a headless Chrome browser: +End-to-end tests are written as [Cucumber](https://cucumber.io/) feature files +and run with [godog](https://github.com/cucumber/godog). Each scenario drives the +real dashboard in a headless Chrome browser (via +[chromedp](https://github.com/chromedp/chromedp)) against an in-process server — +true black-box testing through the UI a user actually sees. The only thing done +outside the browser is invoking a deployed function, which is a public HTTP +endpoint a real client would call directly. + +The feature files in `e2e/features/` read like a product spec (no selectors, no +implementation detail); the step definitions in `e2e/*_test.go` translate that +intent into browser interactions. Make sure Chrome or Chromium is installed before running them. ```bash mise run test-e2e +# Run a single feature while iterating: +GODOG_FEATURE=e2e/features/sign_in.feature go test ./e2e/ ``` -E2E tests cover: +E2E scenarios cover: -- Login flow -- Page navigation -- Functions list rendering +- Signing in and out of the dashboard +- Creating, listing, renaming, and deleting functions +- Invoking functions over HTTP (Lua, Starlark, templates, methods, env vars) +- Editing code, version history, rollback, and version comparison +- Execution history and disabled/deleted-function behaviour +- The key-value store and cron scheduling +- The connected clients page ### Run All Tests diff --git a/docs/adr/0011-testing-strategy.md b/docs/adr/0011-testing-strategy.md index e0e8938..9ec75b4 100644 --- a/docs/adr/0011-testing-strategy.md +++ b/docs/adr/0011-testing-strategy.md @@ -33,14 +33,21 @@ We will test at four layers, each with a dedicated task: 3. **CLI integration tests** (`mise run test-cli-integration`) — built-tagged (`-tags integration`) tests in `lunar-cli/integration` that exercise the generated client against a real running server. -4. **End-to-end browser tests** (`mise run test-e2e`) — Go tests under `e2e/` - that boot the real API on an `httptest.Server` against a migrated SQLite - store and drive **headless Chrome via `chromedp`**, with a small fluent - `browserTest` helper. `mise run test-all` runs units + e2e together. +4. **End-to-end browser tests** (`mise run test-e2e`) — **Cucumber/Gherkin** + feature files under `e2e/features/`, run with + [godog](https://github.com/cucumber/godog). Each scenario boots the real API + on an `httptest.Server` against a migrated SQLite store and drives the + dashboard in **headless Chrome via `chromedp`**. The features are written at + the product level (no selectors, no implementation detail); the Go step + definitions in `e2e/*_test.go` translate that intent into browser + interactions. Function invocation — the one thing a real client does outside + the browser — is exercised over plain HTTP against the public `/fn` endpoint. + `mise run test-all` runs units + e2e together. The unifying principle: wherever a runtime is needed, prefer **Go** as the test -harness (testserver, chromedp, httptest) rather than introducing a second -language's tooling. +harness (testserver, chromedp + godog, httptest) rather than introducing a +second language's tooling. godog keeps the e2e specs in Cucumber while the +harness stays pure Go. ## Consequences diff --git a/e2e/features/connected_clients.feature b/e2e/features/connected_clients.feature new file mode 100644 index 0000000..bffc1e2 --- /dev/null +++ b/e2e/features/connected_clients.feature @@ -0,0 +1,9 @@ +Feature: Connected clients + Operators can review the API clients (such as the CLI) connected to the server. + + Background: + Given I am signed in + + Scenario: Viewing the connected clients page + When I open the connected clients page + Then the connected clients page should be shown diff --git a/e2e/features/execution_history.feature b/e2e/features/execution_history.feature new file mode 100644 index 0000000..b27e8e5 --- /dev/null +++ b/e2e/features/execution_history.feature @@ -0,0 +1,28 @@ +Feature: Execution history + Every call to a function is recorded so operators can audit what happened. + + Background: + Given I am signed in + And I create a function named "audited" + And I open the function "audited" + + Scenario: A function that has never run has no history + When I view the function's execution history + Then the execution history should be empty + + Scenario: Calling a function records an execution + When I call the function + And I view the function's execution history + Then the function should have 1 recorded execution + And the latest execution should be successful + + Scenario: Each call is recorded separately + When I call the function + And I call the function + And I call the function + Then the function should have 3 recorded executions + + Scenario: Inspecting a recorded execution + When I call the function + And I open the most recent execution + Then the execution should be shown as successful diff --git a/e2e/features/invoking_functions.feature b/e2e/features/invoking_functions.feature new file mode 100644 index 0000000..3f501d2 --- /dev/null +++ b/e2e/features/invoking_functions.feature @@ -0,0 +1,66 @@ +Feature: Invoking functions + A deployed function is reachable over HTTP. These scenarios create a function + in the dashboard and then call it the way a real client would. + + Background: + Given I am signed in + + Scenario: A brand-new function responds out of the box + Given I create a function named "hello" + And I open the function "hello" + When I call the function + Then the function should respond successfully + And the response should contain "Hello from Lua!" + + Scenario: A Starlark function responds too + Given I create a Starlark function named "hello-star" + And I open the function "hello-star" + When I call the function + Then the function should respond successfully + And the response should contain "Hello from Starlark!" + + Scenario: A function built from the REST API template responds + Given I create a function named "rest" from the "api" template + And I open the function "rest" + When I call the function + Then the function should respond successfully + + Scenario: A function can be called with different HTTP methods + Given I create a function named "methods" + And I open the function "methods" + And I change the function's code to: + """ + function handler(ctx, event) + return { statusCode = 200, body = event.method } + end + """ + When I call the function with the POST method + Then the function should respond successfully + And the response should be "POST" + + Scenario: A disabled function cannot be called + Given I create a function named "switched-off" + And I open the function "switched-off" + When I disable the function + And I call the function + Then the call should be refused because the function is disabled + + Scenario: A deleted function cannot be called + Given I create a function named "gone" + And I open the function "gone" + When I delete the function + And I call the function + Then the function should no longer be reachable + + Scenario: Environment variables are available to the function + Given I create a function named "greeter-env" + And I open the function "greeter-env" + And I change the function's code to: + """ + function handler(ctx, event) + return { statusCode = 200, body = env.get("GREETING") or "unset" } + end + """ + And I give the function an environment variable "GREETING" set to "hola" + When I call the function + Then the response should be "hola" diff --git a/e2e/features/managing_functions.feature b/e2e/features/managing_functions.feature new file mode 100644 index 0000000..af81b6f --- /dev/null +++ b/e2e/features/managing_functions.feature @@ -0,0 +1,39 @@ +Feature: Managing functions + Operators create, find, rename, and delete functions from the dashboard. + + Background: + Given I am signed in + + Scenario: The functions list starts empty + Given I have not created any functions yet + When I view my functions + Then my functions list should be empty + + Scenario: Creating a function + When I create a function named "greeter" + Then I should land back on my functions list + And I should see "greeter" in my functions list + + Scenario: The list shows which language each function uses + Given I create a function named "lua-one" + And I create a Starlark function named "star-one" + When I view my functions + Then "Lua" should be shown as a supported language + And "Starlark" should be shown as a supported language + + Scenario: A new function needs a name + When I try to create a function without giving it a name + Then I should be told the function needs a name + + Scenario: Renaming a function + Given I create a function named "before-rename" + And I open the function "before-rename" + When I rename the function to "after-rename" + And I view my functions + Then I should see "after-rename" in my functions list + + Scenario: Deleting a function + Given I create a function named "throwaway" + And I open the function "throwaway" + When I delete the function + Then I should no longer see "throwaway" in my functions list diff --git a/e2e/features/sign_in.feature b/e2e/features/sign_in.feature new file mode 100644 index 0000000..84a3119 --- /dev/null +++ b/e2e/features/sign_in.feature @@ -0,0 +1,16 @@ +Feature: Signing in to the dashboard + The dashboard is protected by an API key. Operators sign in to manage their + functions and can sign out when they are done. + + Scenario: Signing in with a valid key + When I sign in with a valid API key + Then I should reach the dashboard + + Scenario: Signing in with an invalid key + When I sign in with an invalid API key + Then I should be told the key is invalid + + Scenario: Signing out + Given I am signed in + When I sign out + Then I should be signed out diff --git a/e2e/features/stored_data.feature b/e2e/features/stored_data.feature new file mode 100644 index 0000000..7d40eef --- /dev/null +++ b/e2e/features/stored_data.feature @@ -0,0 +1,31 @@ +Feature: Function data and scheduling + Functions can keep values in a key-value store and run on a schedule. Both are + configured from the dashboard. + + Background: + Given I am signed in + And I create a function named "stateful" + And I open the function "stateful" + + Scenario: Storing a value in the key-value store + When I store the value "ready" under the key "status" + Then the stored value "ready" should be kept under the key "status" + + Scenario: Storing a value in the global store + When I store the value "shared-secret" under the global key "token" + Then the global value "shared-secret" should be kept under the key "token" + + Scenario: A stored value is readable from the function + Given I store the value "blue" under the key "color" + And I change the function's code to: + """ + function handler(ctx, event) + return { statusCode = 200, body = kv.get("color") or "none" } + end + """ + When I call the function + Then the response should be "blue" + + Scenario: Putting a function on a schedule + When I schedule the function to run every 5 minutes + Then I should see when the function will next run diff --git a/e2e/features/test_page.feature b/e2e/features/test_page.feature new file mode 100644 index 0000000..07e8a9f --- /dev/null +++ b/e2e/features/test_page.feature @@ -0,0 +1,13 @@ +Feature: Trying a function from the dashboard + The dashboard's Test page lets operators send a request to a function and see + the response without leaving the browser. + + Background: + Given I am signed in + And I create a function named "playground" + And I open the function "playground" + + Scenario: Running a function from the Test page + When I try the function from the Test page + Then the Test page should show a successful response + And the Test page response should contain "Hello from Lua!" diff --git a/e2e/features/versions.feature b/e2e/features/versions.feature new file mode 100644 index 0000000..cecb6af --- /dev/null +++ b/e2e/features/versions.feature @@ -0,0 +1,40 @@ +Feature: Working with versions + Every code change is saved as a new version. Operators can review the history, + roll back to an earlier version, and compare two versions side by side. + + Background: + Given I am signed in + And I create a function named "evolving" + And I open the function "evolving" + + Scenario: Editing the code creates a new version + When I change the function's code to: + """ + function handler(ctx, event) + return { statusCode = 200, body = "second cut" } + end + """ + Then the function should have 2 versions + And version 2 should be the active version + + Scenario: Rolling back to an earlier version + Given I change the function's code to: + """ + function handler(ctx, event) + return { statusCode = 200, body = "new behaviour" } + end + """ + When I roll back to version 1 + Then version 1 should be the active version + And I call the function + And the response should contain "Hello from Lua!" + + Scenario: Comparing two versions + Given I change the function's code to: + """ + function handler(ctx, event) + return { statusCode = 200, body = "the changed line" } + end + """ + When I compare version 1 with version 2 + Then I should see the differences between the two versions diff --git a/e2e/function_create_test.go b/e2e/function_create_test.go deleted file mode 100644 index 6918cb8..0000000 --- a/e2e/function_create_test.go +++ /dev/null @@ -1,125 +0,0 @@ -package e2e - -import ( - "testing" - "time" -) - -// TestCreateFunctionWithHTTPTemplate verifies creating a function with default HTTP template -func TestCreateFunctionWithHTTPTemplate(t *testing.T) { - bt := newBrowserTest(t) - functionName := "http-func-" + time.Now().Format("150405") - - bt.Login("#!/functions/new"). - WaitVisible(`#function-name`). - Type(`#function-name`, functionName). - Click(`.create-function-actions button`). - Sleep(1 * time.Second). - AssertURL("#!/functions"). - AssertURLNot("/new"). - AssertFunctionCode(functionName, - "-- HTTP Handler", - "function handler(ctx, event)", - `message = "Hello from Lua!"`, - "event.method", - ) -} - -// TestCreateFunctionWithAPITemplate verifies creating a function with API template -func TestCreateFunctionWithAPITemplate(t *testing.T) { - bt := newBrowserTest(t) - functionName := "api-func-" + time.Now().Format("150405") - - bt.Login("#!/functions/new"). - WaitVisible(`#function-name`). - Type(`#function-name`, functionName). - Click(`.template-card:nth-child(2)`). // Select API template - Sleep(200 * time.Millisecond). - Click(`.create-function-actions button`). - Sleep(1 * time.Second). - AssertURL("#!/functions"). - AssertURLNot("/new"). - AssertFunctionCode(functionName, - "-- REST API Endpoint", - `method == "GET"`, - `method == "POST"`, - "crypto.uuid()", - ). - AssertFunctionCodeNot(functionName, "-- HTTP Handler") -} - -// TestCreateStarlarkFunction verifies selecting the Starlark language produces a -// Starlark function from the chosen template. -func TestCreateStarlarkFunction(t *testing.T) { - bt := newBrowserTest(t) - functionName := "star-func-" + time.Now().Format("150405") - - bt.Login("#!/functions/new"). - WaitVisible(`#function-language`). - Type(`#function-name`, functionName). - SelectOption(`#function-language`, "starlark"). - Sleep(200 * time.Millisecond). - Click(`.create-function-actions button`). - Sleep(1 * time.Second). - AssertURL("#!/functions"). - AssertURLNot("/new"). - AssertFunctionCode(functionName, - "# HTTP Handler", - "def handler(ctx, event):", - `"Hello from Starlark!"`, - ). - AssertFunctionCodeNot(functionName, "function handler(ctx, event)") - - fn := bt.GetFunction(functionName) - if fn == nil || fn.ActiveVersion.Language != "starlark" { - t.Fatalf("expected stored language %q, got %v", "starlark", fn) - } -} - -// TestCreateFunctionValidation verifies validation error for empty name -func TestCreateFunctionValidation(t *testing.T) { - bt := newBrowserTest(t) - - bt.Login("#!/functions/new"). - WaitVisible(`#function-name`). - Click(`.create-function-actions button`). // Submit without name - Sleep(500 * time.Millisecond). - AssertURL("/new") // Should stay on create page - - // Should show error state - hasError := bt.ElementExists(`.form-input--error`) || - bt.ElementExists(`.form-help--error`) || - bt.ElementExists(`.toast--error`) - - if !hasError { - t.Error("expected validation error for empty name") - } -} - -// TestCreateFunctionNavigation verifies navigation to and from the create page -func TestCreateFunctionNavigation(t *testing.T) { - bt := newBrowserTest(t) - - // Navigate from list to create page - bt.Login("#!/functions"). - WaitVisible(`a[href="#!/functions/new"]`). - Click(`a[href="#!/functions/new"]`). - Sleep(500 * time.Millisecond). - AssertURL("/new"). - WaitVisible(`h1.create-function-title`) - - // Cancel button returns to list - bt.Click(`a[href="#!/functions"]`). - Sleep(500 * time.Millisecond). - AssertURL("#!/functions"). - AssertURLNot("/new") - - // Navigate back to create, use back button - bt.Click(`a[href="#!/functions/new"]`). - Sleep(500 * time.Millisecond). - WaitVisible(`.create-function-back a`). - Click(`.create-function-back a`). - Sleep(500 * time.Millisecond). - AssertURL("#!/functions"). - AssertURLNot("/new") -} diff --git a/e2e/function_execution_test.go b/e2e/function_execution_test.go deleted file mode 100644 index 1df1a4c..0000000 --- a/e2e/function_execution_test.go +++ /dev/null @@ -1,322 +0,0 @@ -package e2e - -import ( - "context" - "io" - "net/http" - "strings" - "testing" - - "github.com/dimiro1/lunar/internal/store" -) - -// seedFunction creates a function and an initial active version directly in the -// store, bypassing the UI. Used by execution tests that exercise the runtime -// over HTTP rather than the browser. -func seedFunction(t *testing.T, env *testEnv, id, language, code string) { - t.Helper() - ctx := context.Background() - if _, err := env.Store.CreateFunction(ctx, store.Function{ - ID: id, - Name: id, - EnvVars: map[string]string{}, - }); err != nil { - t.Fatalf("CreateFunction(%q): %v", id, err) - } - if _, err := env.Store.CreateVersion(ctx, id, code, language, nil); err != nil { - t.Fatalf("CreateVersion(%q): %v", id, err) - } -} - -// invoke sends an HTTP request to a function's execution endpoint and returns the -// response and its body. No auth is required for /fn execution. -func invoke(t *testing.T, env *testEnv, method, path, body string, headers map[string]string) (*http.Response, string) { - t.Helper() - req, err := http.NewRequest(method, env.Server.URL+path, strings.NewReader(body)) - if err != nil { - t.Fatalf("new request: %v", err) - } - for k, v := range headers { - req.Header.Set(k, v) - } - resp, err := http.DefaultClient.Do(req) - if err != nil { - t.Fatalf("do request: %v", err) - } - defer func() { _ = resp.Body.Close() }() - b, _ := io.ReadAll(resp.Body) - return resp, string(b) -} - -func TestExecute_Lua_HelloWorld(t *testing.T) { - env := startTestServer(t) - seedFunction(t, env, "lua_hello", "lua", ` -function handler(ctx, event) - return { - statusCode = 200, - headers = { ["Content-Type"] = "text/plain" }, - body = "hello-lua" - } -end`) - - resp, body := invoke(t, env, "GET", "/fn/lua_hello", "", nil) - if resp.StatusCode != 200 { - t.Fatalf("status = %d, want 200 (body: %s)", resp.StatusCode, body) - } - if body != "hello-lua" { - t.Errorf("body = %q, want hello-lua", body) - } - if ct := resp.Header.Get("Content-Type"); ct != "text/plain" { - t.Errorf("Content-Type = %q, want text/plain", ct) - } - if resp.Header.Get("X-Execution-Id") == "" { - t.Error("expected X-Execution-Id header to be set") - } -} - -func TestExecute_Starlark_HelloWorld(t *testing.T) { - env := startTestServer(t) - seedFunction(t, env, "star_hello", "starlark", ` -def handler(ctx, event): - return { - "statusCode": 200, - "headers": {"Content-Type": "text/plain"}, - "body": "hello-starlark", - }`) - - resp, body := invoke(t, env, "GET", "/fn/star_hello", "", nil) - if resp.StatusCode != 200 { - t.Fatalf("status = %d, want 200 (body: %s)", resp.StatusCode, body) - } - if body != "hello-starlark" { - t.Errorf("body = %q, want hello-starlark", body) - } - if ct := resp.Header.Get("Content-Type"); ct != "text/plain" { - t.Errorf("Content-Type = %q, want text/plain", ct) - } -} - -func TestExecute_EventMapping(t *testing.T) { - env := startTestServer(t) - seedFunction(t, env, "lua_echo", "lua", ` -function handler(ctx, event) - return { - statusCode = 200, - body = json.encode({ - method = event.method, - rel = event.relativePath, - q = event.query.foo, - h = event.headers["X-Test"], - body = event.body, - }) - } -end`) - seedFunction(t, env, "star_echo", "starlark", ` -def handler(ctx, event): - body, _ = json.encode({ - "method": event.method, - "rel": event.relativePath, - "q": event.query.get("foo", ""), - "h": event.headers.get("X-Test", ""), - "body": event.body, - }) - return {"statusCode": 200, "body": body}`) - - for _, id := range []string{"lua_echo", "star_echo"} { - resp, body := invoke(t, env, "POST", "/fn/"+id+"/sub/path?foo=bar", - "the-body", map[string]string{"X-Test": "hi"}) - if resp.StatusCode != 200 { - t.Fatalf("%s: status = %d (body: %s)", id, resp.StatusCode, body) - } - for _, want := range []string{ - `"method":"POST"`, - `"rel":"/sub/path"`, - `"q":"bar"`, - `"h":"hi"`, - `"body":"the-body"`, - } { - if !strings.Contains(body, want) { - t.Errorf("%s: body %q missing %q", id, body, want) - } - } - } -} - -func TestExecute_KVPersistsAcrossInvocations(t *testing.T) { - env := startTestServer(t) - seedFunction(t, env, "lua_counter", "lua", ` -function handler(ctx, event) - local n = tonumber(kv.get("c") or "0") + 1 - kv.set("c", tostring(n)) - return { statusCode = 200, body = tostring(n) } -end`) - seedFunction(t, env, "star_counter", "starlark", ` -def handler(ctx, event): - n = int(kv.get("c") or "0") + 1 - kv.set("c", str(n)) - return {"statusCode": 200, "body": str(n)}`) - - for _, id := range []string{"lua_counter", "star_counter"} { - _, first := invoke(t, env, "GET", "/fn/"+id, "", nil) - _, second := invoke(t, env, "GET", "/fn/"+id, "", nil) - if first != "1" || second != "2" { - t.Errorf("%s: counter = %q,%q want 1,2", id, first, second) - } - } -} - -func TestExecute_Starlark_JSONRoundTrip(t *testing.T) { - env := startTestServer(t) - seedFunction(t, env, "star_json", "starlark", ` -def handler(ctx, event): - data, err = json.decode(event.body) - if err != None: - return {"statusCode": 400, "body": err} - out, _ = json.encode({"next": data["n"] + 1}) - return {"statusCode": 200, "body": out}`) - - resp, body := invoke(t, env, "POST", "/fn/star_json", `{"n": 41}`, nil) - if resp.StatusCode != 200 { - t.Fatalf("status = %d (body: %s)", resp.StatusCode, body) - } - if !strings.Contains(body, `"next":42`) { - t.Errorf("body = %q, want next:42", body) - } -} - -func TestExecute_CustomStatusFromHandler(t *testing.T) { - env := startTestServer(t) - seedFunction(t, env, "star_404", "starlark", ` -def handler(ctx, event): - return {"statusCode": 404, "body": "nope"}`) - - resp, body := invoke(t, env, "GET", "/fn/star_404", "", nil) - if resp.StatusCode != 404 { - t.Errorf("status = %d, want 404", resp.StatusCode) - } - if body != "nope" { - t.Errorf("body = %q, want nope", body) - } -} - -func TestExecute_HandlerError(t *testing.T) { - env := startTestServer(t) - seedFunction(t, env, "lua_boom", "lua", - `function handler(ctx, event) error("boom") end`) - seedFunction(t, env, "star_boom", "starlark", - `def handler(ctx, event): - fail("boom")`) - - for _, id := range []string{"lua_boom", "star_boom"} { - resp, _ := invoke(t, env, "GET", "/fn/"+id, "", nil) - if resp.StatusCode != http.StatusInternalServerError { - t.Errorf("%s: status = %d, want 500", id, resp.StatusCode) - } - } -} - -func TestExecute_NotFound(t *testing.T) { - env := startTestServer(t) - resp, _ := invoke(t, env, "GET", "/fn/does-not-exist", "", nil) - if resp.StatusCode != http.StatusNotFound { - t.Errorf("status = %d, want 404", resp.StatusCode) - } -} - -func TestExecute_DisabledFunction(t *testing.T) { - env := startTestServer(t) - seedFunction(t, env, "lua_disabled", "lua", - `function handler(ctx, event) return { statusCode = 200 } end`) - - disabled := true - if err := env.Store.UpdateFunction(context.Background(), "lua_disabled", - store.UpdateFunctionRequest{Disabled: &disabled}); err != nil { - t.Fatalf("UpdateFunction: %v", err) - } - - resp, _ := invoke(t, env, "GET", "/fn/lua_disabled", "", nil) - if resp.StatusCode != http.StatusForbidden { - t.Errorf("status = %d, want 403", resp.StatusCode) - } -} - -func TestExecute_EnvVar(t *testing.T) { - env := startTestServer(t) - seedFunction(t, env, "lua_env", "lua", - `function handler(ctx, event) return { statusCode = 200, body = env.get("GREETING") or "unset" } end`) - seedFunction(t, env, "star_env", "starlark", - `def handler(ctx, event): - return {"statusCode": 200, "body": env.get("GREETING") or "unset"}`) - - if err := env.EnvStore.Set("lua_env", "GREETING", "hola"); err != nil { - t.Fatalf("env set: %v", err) - } - if err := env.EnvStore.Set("star_env", "GREETING", "hej"); err != nil { - t.Fatalf("env set: %v", err) - } - - if _, body := invoke(t, env, "GET", "/fn/lua_env", "", nil); body != "hola" { - t.Errorf("lua env body = %q, want hola", body) - } - if _, body := invoke(t, env, "GET", "/fn/star_env", "", nil); body != "hej" { - t.Errorf("starlark env body = %q, want hej", body) - } -} - -func TestExecute_Router(t *testing.T) { - env := startTestServer(t) - seedFunction(t, env, "star_router", "starlark", ` -def handler(ctx, event): - if router.match(event.relativePath, "/users/:id"): - params = router.params(event.relativePath, "/users/:id") - return {"statusCode": 200, "body": params["id"]} - return {"statusCode": 404, "body": "no match"}`) - - resp, body := invoke(t, env, "GET", "/fn/star_router/users/99", "", nil) - if resp.StatusCode != 200 || body != "99" { - t.Errorf("got %d %q, want 200 99", resp.StatusCode, body) - } - - resp2, _ := invoke(t, env, "GET", "/fn/star_router/posts/1", "", nil) - if resp2.StatusCode != 404 { - t.Errorf("unmatched route status = %d, want 404", resp2.StatusCode) - } -} - -// TestExecute_StarlarkKeywordArgs verifies the Starlark host functions accept -// keyword arguments (a Starlark-only ergonomic the Lua runtime cannot offer). -func TestExecute_StarlarkKeywordArgs(t *testing.T) { - env := startTestServer(t) - seedFunction(t, env, "star_kwargs", "starlark", ` -def handler(ctx, event): - kv.set(key="k", value="v") - return {"statusCode": 200, "body": kv.get(key="k")}`) - - resp, body := invoke(t, env, "GET", "/fn/star_kwargs", "", nil) - if resp.StatusCode != 200 || body != "v" { - t.Errorf("got %d %q, want 200 v", resp.StatusCode, body) - } -} - -// TestExecute_LanguageStickyAcrossVersions verifies that a new version created -// without an explicit language keeps running on the function's language: a -// second Starlark version (language "") must still execute as Starlark. -func TestExecute_LanguageStickyAcrossVersions(t *testing.T) { - env := startTestServer(t) - ctx := context.Background() - seedFunction(t, env, "sticky", "starlark", - `def handler(ctx, event): - return {"statusCode": 200, "body": "v1"}`) - - // New version, no language specified — must inherit Starlark. - if _, err := env.Store.CreateVersion(ctx, "sticky", - `def handler(ctx, event): - return {"statusCode": 200, "body": "v2"}`, "", nil); err != nil { - t.Fatalf("CreateVersion v2: %v", err) - } - - resp, body := invoke(t, env, "GET", "/fn/sticky", "", nil) - if resp.StatusCode != 200 || body != "v2" { - t.Errorf("got %d %q, want 200 v2 (a Lua runtime would have failed to parse)", resp.StatusCode, body) - } -} diff --git a/e2e/function_pages_test.go b/e2e/function_pages_test.go deleted file mode 100644 index 212c9fc..0000000 --- a/e2e/function_pages_test.go +++ /dev/null @@ -1,107 +0,0 @@ -package e2e - -import ( - "context" - "strings" - "testing" - "time" -) - -// assertLangLabel checks the editor's language label case-insensitively (it is -// uppercased by CSS, so the rendered text is e.g. "LUA"). -func assertLangLabel(t *testing.T, bt *browserTest, want string) { - t.Helper() - got := bt.GetText(`.code-editor-lang`) - if !strings.EqualFold(strings.TrimSpace(got), want) { - t.Errorf("editor language label = %q, want %q (any case)", got, want) - } -} - -// TestCodePage_Lua shows the Lua editor language and the Lua API reference. -func TestCodePage_Lua(t *testing.T) { - bt := newBrowserTest(t) - seedFunction(t, bt.env, "page_lua", "lua", - "function handler(ctx, event) return { statusCode = 200 } end") - - bt.Login("#!/functions/page_lua"). - WaitVisible(`.function-details-title`). - AssertElementExists(`.code-editor-container`). - // The handler section (default) types event fields as "table" for Lua. - AssertText(`.api-reference`, "table") - assertLangLabel(t, bt, "lua") -} - -// TestCodePage_Starlark shows the Starlark editor language and the Starlark API -// reference (event fields typed as "dict", not "table"). -func TestCodePage_Starlark(t *testing.T) { - bt := newBrowserTest(t) - seedFunction(t, bt.env, "page_star", "starlark", - "def handler(ctx, event):\n return {\"statusCode\": 200}") - - bt.Login("#!/functions/page_star"). - WaitVisible(`.function-details-title`). - AssertElementExists(`.code-editor-container`). - AssertText(`.api-reference`, "dict") - assertLangLabel(t, bt, "starlark") -} - -// TestVersionsPage_ListsAllVersions verifies every version of a function is -// listed on the versions page. -func TestVersionsPage_ListsAllVersions(t *testing.T) { - bt := newBrowserTest(t) - ctx := context.Background() - seedFunction(t, bt.env, "page_versions", "lua", - "function handler(ctx, event) return { statusCode = 200, body = 'v1' } end") - if _, err := bt.env.Store.CreateVersion(ctx, "page_versions", - "function handler(ctx, event) return { statusCode = 200, body = 'v2' } end", - "", nil); err != nil { - t.Fatalf("CreateVersion v2: %v", err) - } - - bt.Login("#!/functions/page_versions/versions"). - WaitVisible(`tbody tr`). - AssertElementCount(`tbody tr`, 2) -} - -// TestSettingsPage_Renders verifies the settings page renders with the function -// name and the general settings controls. -func TestSettingsPage_Renders(t *testing.T) { - bt := newBrowserTest(t) - seedFunction(t, bt.env, "page_settings", "lua", - "function handler(ctx, event) return { statusCode = 200 } end") - - bt.Login("#!/functions/page_settings/settings"). - WaitVisible(`.function-details-title`). - AssertText(`.function-details-title`, "page_settings"). - AssertElementExists(`#save-response`). - AssertElementExists(`#logRetention`) -} - -// TestExecutionsPage_ShowsInvocations verifies that invoking a function records -// an execution that then appears on the executions page. -func TestExecutionsPage_ShowsInvocations(t *testing.T) { - bt := newBrowserTest(t) - seedFunction(t, bt.env, "page_exec", "lua", - "function handler(ctx, event) return { statusCode = 200, body = 'ok' } end") - - // Two invocations -> two execution records. - invoke(t, bt.env, "GET", "/fn/page_exec", "", nil) - invoke(t, bt.env, "GET", "/fn/page_exec", "", nil) - - bt.Login("#!/functions/page_exec/executions"). - WaitVisible(`tbody tr`). - AssertElementCount(`tbody tr`, 2) -} - -// TestExecutionsPage_Empty verifies the executions page renders for a function -// that has never been invoked (empty state, no crash). -func TestExecutionsPage_Empty(t *testing.T) { - bt := newBrowserTest(t) - seedFunction(t, bt.env, "page_exec_empty", "lua", - "function handler(ctx, event) return { statusCode = 200 } end") - - bt.Login("#!/functions/page_exec_empty/executions"). - WaitVisible(`.function-details-title`). - Sleep(300 * time.Millisecond). - AssertElementCount(`tbody tr`, 0) -} diff --git a/e2e/functions_list_test.go b/e2e/functions_list_test.go deleted file mode 100644 index b7bfff8..0000000 --- a/e2e/functions_list_test.go +++ /dev/null @@ -1,48 +0,0 @@ -package e2e - -import ( - "testing" -) - -// TestFunctionsListAfterLogin verifies functions list displays after authentication -func TestFunctionsListAfterLogin(t *testing.T) { - bt := newBrowserTest(t) - - bt.Login("#!/functions"). - WaitVisible(`h1`). - AssertText(`h1`, "Functions") - - // Verify empty state message or table is present - hasEmptyMessage := bt.ElementExists(`.empty-state`) || - bt.GetHTML("body") != "" // fallback check - hasTable := bt.ElementExists(`table`) - - if !hasEmptyMessage && !hasTable { - t.Error("expected either empty state message or functions table") - } -} - -// TestNewFunctionButtonPresent verifies the new function button is visible -func TestNewFunctionButtonPresent(t *testing.T) { - bt := newBrowserTest(t) - - bt.Login("#!/functions"). - WaitVisible(`a[href="#!/functions/new"]`). - AssertText(`a[href="#!/functions/new"]`, "New Function") -} - -// TestFunctionsListShowsLanguage verifies the list displays each function's -// language alongside its row. -func TestFunctionsListShowsLanguage(t *testing.T) { - bt := newBrowserTest(t) - seedFunction(t, bt.env, "list_lua_fn", "lua", - "function handler(ctx, event) return { statusCode = 200 } end") - seedFunction(t, bt.env, "list_star_fn", "starlark", - "def handler(ctx, event):\n return {\"statusCode\": 200}") - - bt.Login("#!/functions"). - WaitVisible(`table tbody tr`). - AssertTextI(`thead`, "Language"). - AssertTextI(`tbody`, "Lua"). - AssertTextI(`tbody`, "Starlark") -} diff --git a/e2e/godog_test.go b/e2e/godog_test.go new file mode 100644 index 0000000..851a9c8 --- /dev/null +++ b/e2e/godog_test.go @@ -0,0 +1,88 @@ +// Package e2e contains the end-to-end test suite for Lunar, written as +// executable specifications with Cucumber/Gherkin via godog. +// +// The tests are true end-to-end: every scenario drives the real dashboard in a +// headless Chrome browser (via chromedp) against an in-process Lunar server. +// The feature files read like a product spec — they talk about functions, +// versions, and the dashboard, never about selectors or HTTP. The Go step +// definitions translate that intent into browser interactions. The only thing +// done outside the browser is invoking a deployed function, which is a public +// HTTP endpoint a real client would call directly. +// +// Run the whole suite with: +// +// mise run test-e2e # or: go test ./e2e/... +// +// A Chrome/Chromium must be available on the machine. +package e2e + +import ( + "context" + "io" + "log/slog" + "os" + "testing" + + "github.com/cucumber/godog" + "github.com/cucumber/godog/colors" +) + +// TestMain runs the godog suite as part of `go test`. The feature files live in +// features/ and the step bindings are registered in InitializeScenario. +func TestMain(m *testing.M) { + // Silence the server's request/migration logging so the godog report (the + // thing a reader cares about) isn't buried under per-asset HTTP logs. + slog.SetDefault(slog.New(slog.NewTextHandler(io.Discard, nil))) + + // Default to the whole features/ directory; GODOG_FEATURE narrows the run to + // a single file or scenario during development. + paths := []string{"features"} + if p := os.Getenv("GODOG_FEATURE"); p != "" { + paths = []string{p} + } + + opts := godog.Options{ + Format: "pretty", + Paths: paths, + Output: colors.Colored(os.Stdout), + Strict: true, + TestingT: nil, + // Browser scenarios share a single Chrome process and each server is + // per-scenario, so run scenarios sequentially for determinism. + Concurrency: 1, + } + + status := godog.TestSuite{ + Name: "lunar-e2e", + TestSuiteInitializer: InitializeTestSuite, + ScenarioInitializer: InitializeScenario, + Options: &opts, + }.Run() + + os.Exit(status) +} + +// InitializeTestSuite wires suite-level lifecycle hooks. The shared headless +// Chrome process is torn down after every scenario has run. +func InitializeTestSuite(ctx *godog.TestSuiteContext) { + ctx.AfterSuite(func() { + shutdownAllocator() + }) +} + +// InitializeScenario is invoked once per scenario. It creates a fresh world, +// starts a clean server before the scenario, tears everything down after, and +// registers every step binding against that world. +func InitializeScenario(sc *godog.ScenarioContext) { + w := &world{} + + sc.Before(func(ctx context.Context, _ *godog.Scenario) (context.Context, error) { + return ctx, w.setup() + }) + sc.After(func(ctx context.Context, _ *godog.Scenario, _ error) (context.Context, error) { + w.teardown() + return ctx, nil + }) + + w.registerSteps(sc) +} diff --git a/e2e/helpers_test.go b/e2e/helpers_test.go deleted file mode 100644 index c4a6beb..0000000 --- a/e2e/helpers_test.go +++ /dev/null @@ -1,399 +0,0 @@ -package e2e - -import ( - "context" - "database/sql" - "fmt" - "net/http/httptest" - "strings" - "testing" - "time" - - "github.com/chromedp/chromedp" - "github.com/dimiro1/lunar/frontend" - "github.com/dimiro1/lunar/internal/api" - "github.com/dimiro1/lunar/internal/services/env" - internalhttp "github.com/dimiro1/lunar/internal/services/http" - "github.com/dimiro1/lunar/internal/services/kv" - "github.com/dimiro1/lunar/internal/services/logger" - "github.com/dimiro1/lunar/internal/migrate" - "github.com/dimiro1/lunar/internal/store" - _ "modernc.org/sqlite" -) - -const testAPIKey = "test-api-key-12345" - -// testEnv holds the test server and database for e2e tests -type testEnv struct { - Server *httptest.Server - Store *store.SQLiteDB - EnvStore env.Store -} - -// browserTest provides a fluent API for writing e2e tests -type browserTest struct { - t *testing.T - env *testEnv - browserCtx context.Context - cancel context.CancelFunc -} - -// newBrowserTest creates a new browser test with server and browser context -func newBrowserTest(t *testing.T) *browserTest { - t.Helper() - env := startTestServer(t) - // 60s budget per test: generous for local runs, and headroom for slower CI - // runners where Chrome cold-start + Monaco load on the first browser test can - // approach the previous 30s deadline. - ctx, cancel := newBrowserContext(t, 60*time.Second) - - t.Cleanup(func() { - cancel() - }) - - return &browserTest{ - t: t, - env: env, - browserCtx: ctx, - cancel: cancel, - } -} - -// Run executes chromedp actions -func (bt *browserTest) Run(actions ...chromedp.Action) *browserTest { - bt.t.Helper() - if err := chromedp.Run(bt.browserCtx, actions...); err != nil { - bt.t.Fatalf("chromedp run failed: %v", err) - } - return bt -} - -// Login logs in and navigates to the specified path -func (bt *browserTest) Login(path string) *browserTest { - bt.t.Helper() - return bt.Run(loginAndNavigate(bt.env.Server, path)...) -} - -// NavigateTo navigates to a path (assumes already logged in) -func (bt *browserTest) NavigateTo(path string) *browserTest { - bt.t.Helper() - return bt.Run( - chromedp.Navigate(bt.env.Server.URL+path), - chromedp.Sleep(500*time.Millisecond), - ) -} - -// WaitVisible waits for an element to be visible -func (bt *browserTest) WaitVisible(selector string) *browserTest { - bt.t.Helper() - return bt.Run(chromedp.WaitVisible(selector, chromedp.ByQuery)) -} - -// Click clicks an element -func (bt *browserTest) Click(selector string) *browserTest { - bt.t.Helper() - return bt.Run(chromedp.Click(selector, chromedp.ByQuery)) -} - -// Type types text into an input field -func (bt *browserTest) Type(selector, text string) *browserTest { - bt.t.Helper() - return bt.Run(chromedp.SendKeys(selector, text, chromedp.ByQuery)) -} - -// SelectOption sets a