package auth import ( "context" "database/sql" "fmt" "testing" "time" "github.com/wangjia/pangolin/server/internal/config" "github.com/wangjia/pangolin/server/internal/store" ) // openAuthDB spins up an in-memory sqlite DB with the full migration set // (plans get seeded by 000007_seed.up.sql), mirroring reward package's // openDB test harness. Exported (capitalized) so the external store_notices_test.go // (package auth_test, needed to avoid the notices->auth import cycle) can reuse it. func OpenAuthDBForTest(t *testing.T) *sql.DB { t.Helper() db, err := store.Open(&config.Config{Driver: "sqlite", DSN: ":memory:"}) if err != nil { t.Fatal(err) } t.Cleanup(func() { _ = db.Close() }) if err := store.MigrateUp(db, "sqlite"); err != nil { t.Fatal(err) } if err := store.ApplyCodesLibMigrations(context.Background(), db, "sqlite"); err != nil { t.Fatal(err) } return db } // fakeNoticer 记录调用,用于验证「未设置 noticer 时跳过」与「显式 fake 校验参数」两种路径。 // 用 fake(而非真实 notices.Store)是因为 notices 包反向 import auth(handler.go 用 // auth.UserIDFromContext),同包内部测试(package auth)若再 import notices 会成环; // 真实 notices.Store 的集成断言放到外部测试包 store_notices_test.go(package auth_test)。 type fakeNoticer struct { calls int last struct { userID int64 typ, titleZH, titleEN string bodyZH, bodyEN, link string } } func (f *fakeNoticer) InsertNoticeTx(_ context.Context, _ *sql.Tx, userID int64, typ, titleZH, titleEN, bodyZH, bodyEN, link string, _ time.Time) error { f.calls++ f.last.userID = userID f.last.typ = typ f.last.titleZH = titleZH f.last.titleEN = titleEN f.last.bodyZH = bodyZH f.last.bodyEN = bodyEN f.last.link = link return nil } // TestCreateUserWithTrial_NilNoticer_Skips: 未注入 noticer(装配前/未调用 SetNoticer)时, // 注册仍应成功且不 panic —— 保持既有装配可编译、零副作用。 func TestCreateUserWithTrial_NilNoticer_Skips(t *testing.T) { db := OpenAuthDBForTest(t) s := NewSQLStore(db) // 不调用 SetNoticer if _, err := s.CreateUserWithTrial(context.Background(), "nonoticer@example.com", "hash", 7); err != nil { t.Fatalf("CreateUserWithTrial: %v", err) } var n int if err := db.QueryRow(`SELECT COUNT(*) FROM notices`).Scan(&n); err != nil { t.Fatal(err) } if n != 0 { t.Fatalf("notices count = %d, want 0 when noticer not set", n) } } // TestCreateUserWithTrial_FakeNoticer_CalledOnceWithArgs: 用 fake 断言被调用一次、 // 参数与实际 trialDays 一致。 func TestCreateUserWithTrial_FakeNoticer_CalledOnceWithArgs(t *testing.T) { db := OpenAuthDBForTest(t) s := NewSQLStore(db) fn := &fakeNoticer{} s.SetNoticer(fn) u, err := s.CreateUserWithTrial(context.Background(), "fake@example.com", "hash", 14) if err != nil { t.Fatalf("CreateUserWithTrial: %v", err) } if fn.calls != 1 { t.Fatalf("noticer calls = %d, want 1", fn.calls) } if fn.last.userID != u.ID { t.Fatalf("noticer userID = %d, want %d", fn.last.userID, u.ID) } if fn.last.typ != "reward" { t.Fatalf("noticer typ = %q, want reward", fn.last.typ) } wantBodyZH := fmt.Sprintf("已为你开通 %d 天 PRO 会员,现在就选节点连接,畅享全球网络。", 14) if fn.last.bodyZH != wantBodyZH { t.Fatalf("noticer bodyZH = %q, want %q", fn.last.bodyZH, wantBodyZH) } if fn.last.link != "" { t.Fatalf("noticer link = %q, want empty", fn.last.link) } }