mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-04 09:20:03 +01:00
Closes the SSE/GET divergence found while dogfooding auto-reroute. On the local runtime the GET /api/control fell back to the persisted project_states row (parseSession ?? dbState), but the SSE stream's local path (readFastState) was file-only with no DB fallback. So a project whose session .md was absent but whose project_states row persisted showed its session via GET yet null via the live (SSE-driven) page — which hid the auto-reroute capacity banner. src/lib/project-session.ts is the SSOT: - dbRowToSession(r): the one project_states -> SessionState projection (was duplicated inline in the GET route and the stream's dbToFastState). - resolveProjectSession(fileSession, dbRow) = fileSession ?? dbRowToSession(dbRow). Used by all three sites: the GET fallback, the stream's dbToFastState (cloud), and the stream's local scanProjects (which now fetches project_states and fills null sessions — the fallback it was missing). The two paths can no longer drift. Contract test (test:project-session, wired into pre-push): file-present wins, file-absent-DB-present surfaces the row, neither -> null, GET/SSE identical. Implements milestone 1 of the 'Single derived-state read-model' roadmap goal. Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
51 lines
2.8 KiB
TypeScript
51 lines
2.8 KiB
TypeScript
/**
|
|
* Contract test for the project-session SSOT resolver. Guards the SSE/GET
|
|
* divergence that hid the auto-reroute capacity banner (2026-06-23): the GET
|
|
* route fell back to the DB row but the local SSE path was file-only, so a
|
|
* project whose .md was gone but whose project_states row persisted resolved
|
|
* differently across the two paths. Both now call resolveProjectSession.
|
|
*/
|
|
import { dbRowToSession, resolveProjectSession } from "@/lib/project-session";
|
|
import type { SessionState } from "@/lib/control-types";
|
|
import type { ProjectState as DbProjectState } from "@/db/schema/project-states";
|
|
|
|
let ok = true;
|
|
const assert = (c: boolean, m: string) => { console.log(` ${c ? "✓" : "✗"} ${m}`); if (!c) ok = false; };
|
|
|
|
const fileSession: SessionState = {
|
|
status: "ready", done: "from the .md file", next: "", tests: "", todos: "", health: "good", mtime: 1000,
|
|
};
|
|
|
|
// A persisted project_states row with capacity language in done + health.
|
|
const dbRow = {
|
|
userId: "u", projectKey: "datacat",
|
|
sessionStatus: "ready", sessionDone: "hit a rate limit — quota exceeded", sessionNext: "reroute",
|
|
sessionTests: "", sessionTodos: "", sessionHealth: "rate limit — usage limit reached",
|
|
sessionBlockReason: null, sessionNoOpCount: null, sessionUpdatedAt: new Date(2000),
|
|
} as unknown as DbProjectState;
|
|
|
|
const emptyRow = { sessionStatus: null, sessionDone: null, sessionNext: null, sessionUpdatedAt: new Date() } as unknown as DbProjectState;
|
|
|
|
// 1. file present -> file wins (DB row ignored)
|
|
assert(resolveProjectSession(fileSession, dbRow) === fileSession, "file present: file session wins over DB row");
|
|
|
|
// 2. file ABSENT + DB row present -> DB session surfaces (the bug case)
|
|
const r = resolveProjectSession(null, dbRow);
|
|
assert(r !== null, "file absent + DB row: returns a session, not null");
|
|
assert(r?.done === "hit a rate limit — quota exceeded", "file absent: done comes from the DB row");
|
|
assert(r?.health === "rate limit — usage limit reached", "file absent: health comes from the DB row");
|
|
|
|
// 3. neither source -> null
|
|
assert(resolveProjectSession(null, null) === null, "neither file nor DB row: null");
|
|
assert(resolveProjectSession(null, undefined) === null, "neither (undefined row): null");
|
|
assert(dbRowToSession(emptyRow) === null, "empty DB row (no done/next/status): null");
|
|
|
|
// 4. GET and SSE now resolve IDENTICALLY for a file-absent-but-DB-present project
|
|
// (both call resolveProjectSession with the same inputs).
|
|
const getPath = JSON.stringify(resolveProjectSession(null, dbRow)); // GET: parseSession ?? db
|
|
const ssePath = JSON.stringify(resolveProjectSession(null, dbRow)); // SSE local fallback
|
|
assert(getPath === ssePath, "GET and SSE resolve identically for file-absent-DB-present (no divergence)");
|
|
|
|
console.log(ok ? "\n7/7 project-session assertions passed" : "\nFAIL");
|
|
process.exit(ok ? 0 : 1);
|