|
1 | | -import { describe, expect, test } from "bun:test"; |
2 | | -import { isDeferralCurrent } from "../src/cli/star-prompt"; |
| 1 | +import { afterEach, beforeEach, describe, expect, spyOn, test } from "bun:test"; |
| 2 | +import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
| 5 | +import { existsSync } from "node:fs"; |
| 6 | +import { isDeferralCurrent, maybeShowStarPrompt, setStarPromptDepsForTests } from "../src/cli/star-prompt"; |
3 | 7 |
|
4 | 8 | const NOW = Date.parse("2026-08-02T00:00:00.000Z"); |
5 | 9 | const DAY = 24 * 60 * 60 * 1000; |
@@ -34,4 +38,93 @@ describe("isDeferralCurrent", () => { |
34 | 38 | const future = `${new Date(NOW + 30 * DAY).toISOString()} 2.9.1`; |
35 | 39 | expect(isDeferralCurrent(future, "2.10.0", NOW)).toBe(false); |
36 | 40 | }); |
| 41 | + |
| 42 | + test("a future-dated record with a MATCHING version also fails toward re-asking", () => { |
| 43 | + // Clock rollback must not suppress the deferral for the version forever. |
| 44 | + const future = `${new Date(NOW + 30 * DAY).toISOString()} 2.10.0`; |
| 45 | + expect(isDeferralCurrent(future, "2.10.0", NOW)).toBe(false); |
| 46 | + }); |
| 47 | +}); |
| 48 | + |
| 49 | +describe("maybeShowStarPrompt deferral flow (behavior)", () => { |
| 50 | + let home: string; |
| 51 | + const priorHome = process.env.OPENCODEX_HOME; |
| 52 | + const priorThread = process.env.CODEX_THREAD_ID; |
| 53 | + const stdinTTY = process.stdin.isTTY; |
| 54 | + const stdoutTTY = process.stdout.isTTY; |
| 55 | + const AGENT_ENV_VARS = [ |
| 56 | + "CLAUDECODE", "CLAUDE_CODE_ENTRYPOINT", "CLAUDE_CODE_SSE_PORT", |
| 57 | + "CODEX_THREAD_ID", "CODEX_SHELL", "CODEX_CI", "CODEX_SANDBOX", "CODEX_SANDBOX_NETWORK_DISABLED", |
| 58 | + "CURSOR_TRACE_ID", "CURSOR_SESSION_TOKEN", "CURSOR_AGENT", |
| 59 | + "AIDER_CHAT", "OPENCODE_BIN_PATH", "GEMINI_CLI", |
| 60 | + "REPL_ID", "CI", "GITHUB_ACTIONS", "GITLAB_CI", "BUILDKITE", "JENKINS_URL", "TEAMCITY_VERSION", "CODESPACES", |
| 61 | + ]; |
| 62 | + const savedAgentEnv = new Map<string, string | undefined>(); |
| 63 | + |
| 64 | + beforeEach(() => { |
| 65 | + home = mkdtempSync(join(tmpdir(), "ocx-star-deferral-")); |
| 66 | + process.env.OPENCODEX_HOME = home; |
| 67 | + for (const name of AGENT_ENV_VARS) { |
| 68 | + savedAgentEnv.set(name, process.env[name]); |
| 69 | + delete process.env[name]; |
| 70 | + } |
| 71 | + Object.defineProperty(process.stdin, "isTTY", { value: true, configurable: true }); |
| 72 | + Object.defineProperty(process.stdout, "isTTY", { value: true, configurable: true }); |
| 73 | + }); |
| 74 | + |
| 75 | + afterEach(() => { |
| 76 | + setStarPromptDepsForTests(null); |
| 77 | + for (const name of AGENT_ENV_VARS) { |
| 78 | + const value = savedAgentEnv.get(name); |
| 79 | + if (value === undefined) delete process.env[name]; |
| 80 | + else process.env[name] = value; |
| 81 | + } |
| 82 | + Object.defineProperty(process.stdin, "isTTY", { value: stdinTTY, configurable: true }); |
| 83 | + Object.defineProperty(process.stdout, "isTTY", { value: stdoutTTY, configurable: true }); |
| 84 | + if (priorThread === undefined) delete process.env.CODEX_THREAD_ID; |
| 85 | + else process.env.CODEX_THREAD_ID = priorThread; |
| 86 | + if (priorHome === undefined) delete process.env.OPENCODEX_HOME; |
| 87 | + else process.env.OPENCODEX_HOME = priorHome; |
| 88 | + rmSync(home, { recursive: true, force: true }); |
| 89 | + }); |
| 90 | + |
| 91 | + test("agent deferral fires once per version, never writes the marker, and a human run still prompts", async () => { |
| 92 | + process.env.CODEX_THREAD_ID = "agent-session"; |
| 93 | + setStarPromptDepsForTests({ |
| 94 | + ghAvailable: () => true, |
| 95 | + interactiveConfirm: async () => false, |
| 96 | + }); |
| 97 | + const log = spyOn(console, "log").mockImplementation(() => {}); |
| 98 | + try { |
| 99 | + await maybeShowStarPrompt(); |
| 100 | + const firstCalls = log.mock.calls.length; |
| 101 | + expect(firstCalls).toBeGreaterThan(0); |
| 102 | + // The deferral record exists; the one-time marker does NOT. |
| 103 | + expect(existsSync(join(home, ".star-deferred"))).toBe(true); |
| 104 | + expect(existsSync(join(home, ".star-prompted"))).toBe(false); |
| 105 | + expect(readFileSync(join(home, ".star-deferred"), "utf-8")).toContain(" "); |
| 106 | + |
| 107 | + // Second agent-driven start: suppressed by the record. |
| 108 | + log.mockClear(); |
| 109 | + await maybeShowStarPrompt(); |
| 110 | + expect(log.mock.calls.length).toBe(0); |
| 111 | + expect(existsSync(join(home, ".star-prompted"))).toBe(false); |
| 112 | + |
| 113 | + // A hand-typed run still gets the real question (marker written, ask called). |
| 114 | + delete process.env.CODEX_THREAD_ID; |
| 115 | + let asked = 0; |
| 116 | + setStarPromptDepsForTests({ |
| 117 | + ghAvailable: () => true, |
| 118 | + interactiveConfirm: async () => { |
| 119 | + asked += 1; |
| 120 | + return false; |
| 121 | + }, |
| 122 | + }); |
| 123 | + await maybeShowStarPrompt(); |
| 124 | + expect(asked).toBe(1); |
| 125 | + expect(existsSync(join(home, ".star-prompted"))).toBe(true); |
| 126 | + } finally { |
| 127 | + log.mockRestore(); |
| 128 | + } |
| 129 | + }); |
37 | 130 | }); |
0 commit comments