diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-02-16 22:49:07 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-02-16 22:49:07 +0900 |
| commit | efe05c1444963c046ab91bf54fa51a794bda58c0 (patch) | |
| tree | 509b48f27d2e888740bea6bfd6f50895705c7472 /worker/swift/handlers_test.go | |
| parent | db87f85aa7055e597800481b8cc6d006c70bcc88 (diff) | |
| download | phperkaigi-2026-albatross-efe05c1444963c046ab91bf54fa51a794bda58c0.tar.gz phperkaigi-2026-albatross-efe05c1444963c046ab91bf54fa51a794bda58c0.tar.zst phperkaigi-2026-albatross-efe05c1444963c046ab91bf54fa51a794bda58c0.zip | |
test(worker): add unit tests for php and swift workers
Extract testable logic from exec.mjs into lib.mjs (preprocessCode,
createIOCallbacks, buildResult) and add vitest tests. Add Go tests
for models, exec helpers, and handlers in worker/swift. Update
justfiles to include test tasks for local dev and CI.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Diffstat (limited to 'worker/swift/handlers_test.go')
| -rw-r--r-- | worker/swift/handlers_test.go | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/worker/swift/handlers_test.go b/worker/swift/handlers_test.go new file mode 100644 index 0000000..f9c2e0b --- /dev/null +++ b/worker/swift/handlers_test.go @@ -0,0 +1,66 @@ +package main + +import ( + "errors" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "github.com/labstack/echo/v4" +) + +func TestNewBadRequestError(t *testing.T) { + httpErr := newBadRequestError(errors.New("test error")) + if httpErr.Code != http.StatusBadRequest { + t.Errorf("status code = %d, want %d", httpErr.Code, http.StatusBadRequest) + } + msg, ok := httpErr.Message.(string) + if !ok { + t.Fatalf("expected string message, got %T", httpErr.Message) + } + if !strings.Contains(msg, "test error") { + t.Errorf("message = %q, want it to contain %q", msg, "test error") + } +} + +func TestHandleExec_InvalidJSON(t *testing.T) { + e := echo.New() + req := httptest.NewRequest(http.MethodPost, "/exec", strings.NewReader("not json")) + req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) + rec := httptest.NewRecorder() + c := e.NewContext(req, rec) + + err := handleExec(c) + if err == nil { + t.Fatal("expected error, got nil") + } + httpErr, ok := err.(*echo.HTTPError) + if !ok { + t.Fatalf("expected *echo.HTTPError, got %T", err) + } + if httpErr.Code != http.StatusBadRequest { + t.Errorf("status code = %d, want %d", httpErr.Code, http.StatusBadRequest) + } +} + +func TestHandleExec_ZeroMaxDuration(t *testing.T) { + e := echo.New() + body := `{"code":"print(1)","code_hash":"abc","stdin":"","max_duration_ms":0}` + req := httptest.NewRequest(http.MethodPost, "/exec", strings.NewReader(body)) + req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) + rec := httptest.NewRecorder() + c := e.NewContext(req, rec) + + err := handleExec(c) + if err == nil { + t.Fatal("expected error, got nil") + } + httpErr, ok := err.(*echo.HTTPError) + if !ok { + t.Fatalf("expected *echo.HTTPError, got %T", err) + } + if httpErr.Code != http.StatusBadRequest { + t.Errorf("status code = %d, want %d", httpErr.Code, http.StatusBadRequest) + } +} |
