aboutsummaryrefslogtreecommitdiffhomepage
path: root/backend/game/hub_test.go
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2026-02-15 22:13:50 +0900
committernsfisis <nsfisis@gmail.com>2026-02-15 22:16:22 +0900
commit5ed369a6c70707543fd5ec9a13c79851fdfc5d6c (patch)
treee5678d6d88fab3ac0ae8c05b85236f3e7d5eddfd /backend/game/hub_test.go
parent87e9f5ed48af3a8dca5f6373ae900336f285eef5 (diff)
downloadphperkaigi-2026-albatross-5ed369a6c70707543fd5ec9a13c79851fdfc5d6c.tar.gz
phperkaigi-2026-albatross-5ed369a6c70707543fd5ec9a13c79851fdfc5d6c.tar.zst
phperkaigi-2026-albatross-5ed369a6c70707543fd5ec9a13c79851fdfc5d6c.zip
refactor(backend): introduce DI interfaces for testability
Replace concrete *db.Queries and *pgxpool.Pool dependencies with db.Querier and db.TxManager interfaces across all handlers, game hub, and auth. This enables unit testing with mocks. - Enable sqlc emit_interface to generate Querier interface - Add TxManager abstraction to encapsulate transactions - Convert auth package-level functions to Authenticator struct - Add TaskQueueInterface/TaskWorkerInterface for game.Hub - Add initial unit tests for game logic and API handlers Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Diffstat (limited to 'backend/game/hub_test.go')
-rw-r--r--backend/game/hub_test.go100
1 files changed, 100 insertions, 0 deletions
diff --git a/backend/game/hub_test.go b/backend/game/hub_test.go
new file mode 100644
index 0000000..a8fad58
--- /dev/null
+++ b/backend/game/hub_test.go
@@ -0,0 +1,100 @@
+package game
+
+import "testing"
+
+func TestCalcCodeSize_PHP(t *testing.T) {
+ hub := &Hub{}
+ tests := []struct {
+ name string
+ code string
+ language string
+ want int
+ }{
+ {
+ name: "simple php code",
+ code: "<?php echo 1;",
+ language: "php",
+ want: 6, // "echo1;" after stripping whitespace and "<?php"
+ },
+ {
+ name: "php with short open tag",
+ code: "<? echo 1;",
+ language: "php",
+ want: 6, // "echo1;" after stripping whitespace and "<?"
+ },
+ {
+ name: "php with closing tag",
+ code: "<?php echo 1; ?>",
+ language: "php",
+ want: 6, // "echo1;" after stripping whitespace, "<?php", and "?>"
+ },
+ {
+ name: "php with whitespace",
+ code: "<?php echo 1 ; ?>",
+ language: "php",
+ want: 6,
+ },
+ {
+ name: "non-php language",
+ code: "print(1)",
+ language: "swift",
+ want: 8,
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ got := hub.CalcCodeSize(tt.code, tt.language)
+ if got != tt.want {
+ t.Errorf("CalcCodeSize(%q, %q) = %d, want %d", tt.code, tt.language, got, tt.want)
+ }
+ })
+ }
+}
+
+func TestIsTestcaseResultCorrect(t *testing.T) {
+ tests := []struct {
+ name string
+ expected string
+ actual string
+ want bool
+ }{
+ {
+ name: "exact match",
+ expected: "hello",
+ actual: "hello",
+ want: true,
+ },
+ {
+ name: "trailing newline ignored",
+ expected: "hello\n",
+ actual: "hello",
+ want: true,
+ },
+ {
+ name: "CRLF normalized",
+ expected: "hello\r\n",
+ actual: "hello\n",
+ want: true,
+ },
+ {
+ name: "mismatch",
+ expected: "hello",
+ actual: "world",
+ want: false,
+ },
+ {
+ name: "multiline match",
+ expected: "line1\nline2",
+ actual: "line1\nline2\n",
+ want: true,
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ got := isTestcaseResultCorrect(tt.expected, tt.actual)
+ if got != tt.want {
+ t.Errorf("isTestcaseResultCorrect(%q, %q) = %v, want %v", tt.expected, tt.actual, got, tt.want)
+ }
+ })
+ }
+}