|
| 1 | +import * as fs from "node:fs"; |
| 2 | +import * as os from "node:os"; |
| 3 | +import * as path from "node:path"; |
| 4 | +import { afterAll, beforeAll, describe, expect, test } from "vitest"; |
| 5 | +import { |
| 6 | + GIT_COMPRESSIBLE_SUBCOMMANDS, |
| 7 | + RTK_PLAIN_COMMANDS, |
| 8 | +} from "./claude/session/rtk"; |
| 9 | +import { appendRtkGuidanceForCodex, buildRtkGuidance } from "./rtk-guidance"; |
| 10 | + |
| 11 | +describe("rtk guidance for codex", () => { |
| 12 | + let dir: string; |
| 13 | + let binary: string; |
| 14 | + |
| 15 | + beforeAll(() => { |
| 16 | + dir = fs.mkdtempSync(path.join(os.tmpdir(), "rtk-guidance-test-")); |
| 17 | + binary = path.join(dir, "rtk"); |
| 18 | + fs.writeFileSync(binary, "#!/bin/sh\n"); |
| 19 | + }); |
| 20 | + |
| 21 | + afterAll(() => { |
| 22 | + fs.rmSync(dir, { recursive: true, force: true }); |
| 23 | + }); |
| 24 | + |
| 25 | + describe("buildRtkGuidance", () => { |
| 26 | + // The guidance must advertise exactly the Claude hook's eligibility sets, |
| 27 | + // so the token-usage cohorts stay comparable across adapters. |
| 28 | + test("advertises every command the Claude hook rewrites", () => { |
| 29 | + const guidance = buildRtkGuidance("/usr/local/bin/rtk"); |
| 30 | + for (const command of RTK_PLAIN_COMMANDS) { |
| 31 | + expect(guidance).toContain(command); |
| 32 | + } |
| 33 | + for (const sub of GIT_COMPRESSIBLE_SUBCOMMANDS) { |
| 34 | + expect(guidance).toContain(sub); |
| 35 | + } |
| 36 | + }); |
| 37 | + |
| 38 | + test("uses the resolved binary path in the examples", () => { |
| 39 | + const guidance = buildRtkGuidance("/usr/local/bin/rtk"); |
| 40 | + expect(guidance).toContain("`/usr/local/bin/rtk git status`"); |
| 41 | + }); |
| 42 | + |
| 43 | + // A desktop install can resolve a path with spaces; unquoted it would |
| 44 | + // split into multiple shell tokens and every guided command would fail. |
| 45 | + test("shell-quotes a binary path containing spaces", () => { |
| 46 | + const guidance = buildRtkGuidance("/Apps/PostHog Code/rtk"); |
| 47 | + expect(guidance).toContain("`'/Apps/PostHog Code/rtk' git status`"); |
| 48 | + expect(guidance).not.toContain("`/Apps/PostHog Code/rtk git status`"); |
| 49 | + }); |
| 50 | + |
| 51 | + // Parity with the Claude hook's exclusion: prefixing commit/push would |
| 52 | + // hide the leading `git` token from the cloud signed-commit guard. |
| 53 | + test("forbids prefixing git commit and git push", () => { |
| 54 | + const guidance = buildRtkGuidance("rtk"); |
| 55 | + expect(guidance).toContain("Never prefix `git commit`, `git push`"); |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + describe("appendRtkGuidanceForCodex", () => { |
| 60 | + test("appends guidance when rtk is on PATH", () => { |
| 61 | + const result = appendRtkGuidanceForCodex("base instructions", { |
| 62 | + PATH: dir, |
| 63 | + }); |
| 64 | + expect(result.startsWith("base instructions\n\n")).toBe(true); |
| 65 | + expect(result).toContain("rtk command-output compression"); |
| 66 | + expect(result).toContain(binary); |
| 67 | + }); |
| 68 | + |
| 69 | + // POSTHOG_RTK=0 is set per run from the cloud kill-switch flag; it must |
| 70 | + // silence the guidance too, which is why the gate is resolveRtkPrefix |
| 71 | + // rather than detectRtkBinary. |
| 72 | + test.each([["0"], ["false"]])( |
| 73 | + "returns instructions unchanged when POSTHOG_RTK is %s", |
| 74 | + (value) => { |
| 75 | + expect( |
| 76 | + appendRtkGuidanceForCodex("base instructions", { |
| 77 | + POSTHOG_RTK: value, |
| 78 | + PATH: dir, |
| 79 | + }), |
| 80 | + ).toBe("base instructions"); |
| 81 | + }, |
| 82 | + ); |
| 83 | + |
| 84 | + test("returns instructions unchanged when rtk is not installed", () => { |
| 85 | + expect( |
| 86 | + appendRtkGuidanceForCodex("base instructions", { |
| 87 | + PATH: "/nonexistent", |
| 88 | + }), |
| 89 | + ).toBe("base instructions"); |
| 90 | + }); |
| 91 | + |
| 92 | + test("does not leave a leading separator when instructions are empty", () => { |
| 93 | + const result = appendRtkGuidanceForCodex("", { PATH: dir }); |
| 94 | + expect(result.startsWith("## rtk")).toBe(true); |
| 95 | + }); |
| 96 | + }); |
| 97 | +}); |
0 commit comments