|
| 1 | +// tested with: claude code v2.1.133 + bun 1.3 |
| 2 | + |
| 3 | +import { Database } from "bun:sqlite"; |
| 4 | +import { afterEach, describe, expect, it } from "bun:test"; |
| 5 | +import { mkdtempSync, mkdirSync, rmSync } from "node:fs"; |
| 6 | +import { tmpdir } from "node:os"; |
| 7 | +import { join } from "node:path"; |
| 8 | + |
| 9 | +const fixtures: string[] = []; |
| 10 | +const script = join(import.meta.dir, "..", "bin", "cc-quick"); |
| 11 | + |
| 12 | +afterEach(() => { |
| 13 | + for (const fixture of fixtures.splice(0)) { |
| 14 | + rmSync(fixture, { recursive: true, force: true }); |
| 15 | + } |
| 16 | +}); |
| 17 | + |
| 18 | +function makeFixture(): { |
| 19 | + root: string; |
| 20 | + dbPath: string; |
| 21 | + now: number; |
| 22 | +} { |
| 23 | + const root = mkdtempSync(join(tmpdir(), "cc-quick-")); |
| 24 | + fixtures.push(root); |
| 25 | + const ccDir = join(root, "channels", "cc"); |
| 26 | + mkdirSync(ccDir, { recursive: true }); |
| 27 | + const dbPath = join(ccDir, "sessions.db"); |
| 28 | + const db = new Database(dbPath); |
| 29 | + db.exec(` |
| 30 | + CREATE TABLE sessions ( |
| 31 | + id TEXT PRIMARY KEY, |
| 32 | + name TEXT, |
| 33 | + cwd TEXT, |
| 34 | + project_root TEXT, |
| 35 | + branch TEXT, |
| 36 | + worktree_root TEXT, |
| 37 | + role TEXT, |
| 38 | + pid INTEGER, |
| 39 | + started_at_ms INTEGER NOT NULL, |
| 40 | + last_seen_at_ms INTEGER NOT NULL, |
| 41 | + last_checked_at_ms INTEGER, |
| 42 | + ended_at_ms INTEGER |
| 43 | + ); |
| 44 | + CREATE TABLE announcements ( |
| 45 | + id TEXT PRIMARY KEY, |
| 46 | + session_id TEXT NOT NULL, |
| 47 | + summary TEXT NOT NULL, |
| 48 | + detail TEXT, |
| 49 | + created_at_ms INTEGER NOT NULL |
| 50 | + ); |
| 51 | + CREATE TABLE recent_files ( |
| 52 | + session_id TEXT NOT NULL, |
| 53 | + path TEXT NOT NULL, |
| 54 | + touched_at_ms INTEGER NOT NULL |
| 55 | + ); |
| 56 | + `); |
| 57 | + const now = Date.now(); |
| 58 | + const insert = db.prepare( |
| 59 | + `INSERT INTO sessions |
| 60 | + (id, cwd, branch, started_at_ms, last_seen_at_ms, ended_at_ms) |
| 61 | + VALUES (?, ?, ?, ?, ?, NULL)`, |
| 62 | + ); |
| 63 | + insert.run("fresh-session", "/tmp/fresh", "main", now - 1000, now - 1000); |
| 64 | + insert.run("stale-session", "/tmp/stale", "main", now - 600000, now - 600000); |
| 65 | + db.close(); |
| 66 | + return { root, dbPath, now }; |
| 67 | +} |
| 68 | + |
| 69 | +describe("cc-quick live roster", () => { |
| 70 | + it("shows fresh rows and marks stale rows ended", () => { |
| 71 | + const fixture = makeFixture(); |
| 72 | + const result = Bun.spawnSync(["bash", script, "roster"], { |
| 73 | + env: { |
| 74 | + ...process.env, |
| 75 | + CLAUDE_CODE_SESSION_ID: "fixture-caller", |
| 76 | + CLAUDE_CONFIG_DIR: fixture.root, |
| 77 | + CC_STALE_SESSION_MS: "300000", |
| 78 | + }, |
| 79 | + }); |
| 80 | + |
| 81 | + expect(result.exitCode).toBe(0); |
| 82 | + const output = result.stdout.toString(); |
| 83 | + expect(output).toContain("fresh-se"); |
| 84 | + expect(output).not.toContain("stale-se"); |
| 85 | + |
| 86 | + const db = new Database(fixture.dbPath); |
| 87 | + const stale = db |
| 88 | + .query("SELECT last_seen_at_ms, ended_at_ms FROM sessions WHERE id = ?") |
| 89 | + .get("stale-session") as { |
| 90 | + last_seen_at_ms: number; |
| 91 | + ended_at_ms: number | null; |
| 92 | + }; |
| 93 | + expect(stale.ended_at_ms).toBe(stale.last_seen_at_ms); |
| 94 | + db.close(); |
| 95 | + }); |
| 96 | + |
| 97 | + it("rejects a non-numeric stale-session window", () => { |
| 98 | + const fixture = makeFixture(); |
| 99 | + const result = Bun.spawnSync(["bash", script, "roster"], { |
| 100 | + env: { |
| 101 | + ...process.env, |
| 102 | + CLAUDE_CODE_SESSION_ID: "fixture-caller", |
| 103 | + CLAUDE_CONFIG_DIR: fixture.root, |
| 104 | + CC_STALE_SESSION_MS: "unsafe", |
| 105 | + }, |
| 106 | + }); |
| 107 | + |
| 108 | + expect(result.exitCode).toBe(2); |
| 109 | + expect(result.stderr.toString()).toContain( |
| 110 | + "CC_STALE_SESSION_MS must be a positive integer", |
| 111 | + ); |
| 112 | + }); |
| 113 | +}); |
0 commit comments