console last seen tests: a failed write keeps the hour; stricter reads
lastSeenOf treats only sql.ErrNoRows as "no row" and fails on any other error; the users snapshot checks rows.Err(). New regression test: with a trigger refusing every last_seen UPDATE, the request still answers, the write is attempted once, and the next request within the hour makes no new attempt. The missing-user test cites controller ruling 2026-10-26.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 file changed,
+50
−3
internal/console/lastseen_test.go+50 −3
CI failed — run details
diff --git a/internal/console/lastseen_test.go b/internal/console/lastseen_test.go| index 0313e0606..5c551d55c 100644 |
| --- a/internal/console/lastseen_test.go |
| +++ b/internal/console/lastseen_test.go |
| @@ -1,6 +1,8 @@ |
| package console |
| import ( |
| + "database/sql" |
| + "errors" |
| "net/http" |
| "sync" |
| "testing" |
| @@ -13,13 +15,16 @@ import ( |
| const lastSeenPaul = "paul@keymail.dev" |
| // lastSeenOf reads address's stored last_seen (Unix seconds); ok is false |
| -// when there is no users row. |
| +// when there is no users row. Any other error fails the test. |
| func lastSeenOf(t *testing.T, fx *consoleFixture, address string) (seen int64, ok bool) { |
| t.Helper() |
| err := fx.st.db.QueryRow(`SELECT last_seen FROM users WHERE address = ?`, address).Scan(&seen) |
| - if err != nil { |
| + if errors.Is(err, sql.ErrNoRows) { |
| return 0, false |
| } |
| + if err != nil { |
| + t.Fatalf("read last_seen of %s: %v", address, err) |
| + } |
| return seen, true |
| } |
| @@ -175,6 +180,9 @@ func TestLastSeenFromTheCLI(t *testing.T) { |
| } |
| out[a] = s |
| } |
| + if err := rows.Err(); err != nil { |
| + t.Fatal(err) |
| + } |
| return out |
| } |
| before := snapshot() |
| @@ -222,7 +230,8 @@ func TestLastSeenWithNoUserRowIsHarmless(t *testing.T) { |
| // The page's own landing-spot upsert (SetLastAccount) has always made a |
| // users row for a session without one, and it runs after the gate. The |
| // touch ran first, against no row, so a row the page made carries no |
| - // last_seen. |
| + // last_seen. That upsert stays as it is (controller ruling 2026-10-26): |
| + // this test asserts last_seen 0 on that row, not that no row exists. |
| if seen, ok := lastSeenOf(t, fx, lastSeenPaul); ok && seen != 0 { |
| t.Fatalf("last_seen = %d on a row the touch found missing, want 0", seen) |
| } |
| @@ -246,3 +255,41 @@ func TestLastSeenWithNoUserRowIsHarmless(t *testing.T) { |
| t.Fatal("touching last_seen created a users row") |
| } |
| } |
| + |
| +// TestLastSeenFailedWriteKeepsTheHour: when the last_seen UPDATE fails, the |
| +// request still answers, the failure is not retried on every request, and |
| +// the hour stays reserved: the next request within it makes no new attempt. |
| +func TestLastSeenFailedWriteKeepsTheHour(t *testing.T) { |
| + fx := newConsoleFixture(t) |
| + c := signInPaul(t, fx) |
| + want := getKass(t, fx, c) |
| + // Expire the hour and move last_seen back first, so the next UPDATE |
| + // matches the row and reaches the trigger below. |
| + expireSeen(t, fx, lastSeenPaul, true) |
| + // Every write to users.last_seen now fails inside the database, the |
| + // same error path a locked or broken database takes. |
| + if _, err := fx.st.db.Exec(`CREATE TRIGGER lastseen_refused BEFORE UPDATE OF last_seen ON users |
| +BEGIN SELECT RAISE(ABORT, 'test: last_seen refused'); END;`); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := fx.st.TouchLastSeen(lastSeenPaul, time.Now()); err == nil { |
| + t.Fatal("TouchLastSeen succeeded past the refusing trigger; the test would not exercise a failed write") |
| + } |
| + before, _ := lastSeenOf(t, fx, lastSeenPaul) |
| + base := fx.srv.lastSeenWrites.Load() |
| + if got := getKass(t, fx, c); got != want { |
| + t.Fatalf("GET /apps/kass with a failing last_seen write = %d, want %d", got, want) |
| + } |
| + if got := fx.srv.lastSeenWrites.Load() - base; got != 1 { |
| + t.Fatalf("lastSeenWrites after the hour = +%d, want +1", got) |
| + } |
| + if after, _ := lastSeenOf(t, fx, lastSeenPaul); after != before { |
| + t.Fatalf("last_seen moved %d -> %d through a failing write", before, after) |
| + } |
| + if got := getKass(t, fx, c); got != want { |
| + t.Fatalf("second GET /apps/kass = %d, want %d", got, want) |
| + } |
| + if got := fx.srv.lastSeenWrites.Load() - base; got != 1 { |
| + t.Fatalf("lastSeenWrites after a second GET within the hour = +%d, want +1 (the failed write's reservation kept)", got) |
| + } |
| +} |