From efe05c1444963c046ab91bf54fa51a794bda58c0 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Mon, 16 Feb 2026 22:49:07 +0900 Subject: 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 --- worker/swift/models_test.go | 70 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 worker/swift/models_test.go (limited to 'worker/swift/models_test.go') diff --git a/worker/swift/models_test.go b/worker/swift/models_test.go new file mode 100644 index 0000000..62d6675 --- /dev/null +++ b/worker/swift/models_test.go @@ -0,0 +1,70 @@ +package main + +import ( + "testing" + "time" +) + +func TestExecRequestData_Validate(t *testing.T) { + tests := []struct { + name string + maxDurationMs int + wantErr error + }{ + {"positive value", 1000, nil}, + {"zero", 0, errInvalidMaxDuration}, + {"negative value", -1, errInvalidMaxDuration}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + req := &execRequestData{MaxDurationMilliseconds: tt.maxDurationMs} + err := req.validate() + if err != tt.wantErr { + t.Errorf("validate() = %v, want %v", err, tt.wantErr) + } + }) + } +} + +func TestExecRequestData_MaxDuration(t *testing.T) { + tests := []struct { + name string + maxDurationMs int + want time.Duration + }{ + {"1000ms", 1000, 1 * time.Second}, + {"500ms", 500, 500 * time.Millisecond}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + req := &execRequestData{MaxDurationMilliseconds: tt.maxDurationMs} + got := req.maxDuration() + if got != tt.want { + t.Errorf("maxDuration() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestExecResponseData_Success(t *testing.T) { + tests := []struct { + name string + status string + want bool + }{ + {"success", resultSuccess, true}, + {"compile_error", resultCompileError, false}, + {"runtime_error", resultRuntimeError, false}, + {"timeout", resultTimeout, false}, + {"internal_error", resultInternalError, false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + res := &execResponseData{Status: tt.status} + got := res.success() + if got != tt.want { + t.Errorf("success() = %v, want %v", got, tt.want) + } + }) + } +} -- cgit v1.3.1