package api import ( "context" "fmt" "net/http" "net/http/httptest" "strings" "tera-clone/services/tera-api/internal/startup" "testing" "time" ) func TestBootstrapTimeAvailableBeforeLoginButNotReplayable(t *testing.T) { now := time.Unix(0, 0) clock, _ := startup.NewClockBootstrap(func() time.Time { return now }, func(_ context.Context, v time.Time) error { now = v; return nil }) token := clock.Token() router := NewRouterWithOptions(newPasswordAuthTestStore(t, fixedClock), RouterOptions{CommandsReady: func() bool { return false }, ClockBootstrap: clock}) status := httptest.NewRecorder() router.ServeHTTP(status, httptest.NewRequest("GET", "/api/v1/auth/startup", nil)) if !strings.Contains(status.Body.String(), token) { t.Fatal("missing bootstrap token") } body := fmt.Sprintf(`{"token":%q,"browser_time":"2026-09-09T01:00:00Z"}`, token) for i, want := range []int{http.StatusOK, http.StatusConflict} { response := httptest.NewRecorder() router.ServeHTTP(response, httptest.NewRequest("POST", "/api/v1/auth/bootstrap-time", strings.NewReader(body))) if response.Code != want { t.Fatalf("attempt %d: %d %s", i, response.Code, response.Body.String()) } } login := httptest.NewRecorder() router.ServeHTTP(login, httptest.NewRequest("POST", "/api/v1/auth/login", strings.NewReader(`{}`))) if login.Code != http.StatusServiceUnavailable { t.Fatal("clock sync bypassed startup readiness") } }