|
| 1 | +import { afterEach, describe, expect, test } from "bun:test" |
| 2 | +import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs" |
| 3 | +import { tmpdir } from "node:os" |
| 4 | +import { dirname, join } from "node:path" |
| 5 | +import { ENGINE_PREPARED_STAMP, ensureEnginePrepared } from "../bin/lib/engine-prepare.js" |
| 6 | + |
| 7 | +const PI_AI_MESSAGES = "node_modules/@earendil-works/pi-ai/dist/api/anthropic-messages.js" |
| 8 | +const RPC_MODE = "dist/modes/rpc/rpc-mode.js" |
| 9 | +const OMO_VERSION = "5.0.0-0.beta.86" |
| 10 | +const REINSTALL = "npm i -g omo-ai@beta" |
| 11 | + |
| 12 | +const roots: string[] = [] |
| 13 | + |
| 14 | +function uaSource(version: string): string { |
| 15 | + return `const claudeCodeVersion = "${version}";\nexport {}\n` |
| 16 | +} |
| 17 | + |
| 18 | +function createEngine(uaVersion: string): string { |
| 19 | + const root = mkdtempSync(join(tmpdir(), "omo-engine-prepare-")) |
| 20 | + roots.push(root) |
| 21 | + writeFileSync(join(root, "package.json"), JSON.stringify({ name: "@code-yeongyu/senpi", version: "2026.9.23", type: "module" })) |
| 22 | + const rpcPath = join(root, RPC_MODE) |
| 23 | + mkdirSync(dirname(rpcPath), { recursive: true }) |
| 24 | + writeFileSync(rpcPath, readFileSync(new URL("./modes/rpc/rpc-mode.js", import.meta.resolve("@code-yeongyu/senpi")), "utf8")) |
| 25 | + const messages = join(root, PI_AI_MESSAGES) |
| 26 | + mkdirSync(dirname(messages), { recursive: true }) |
| 27 | + writeFileSync(messages, uaSource(uaVersion)) |
| 28 | + return root |
| 29 | +} |
| 30 | + |
| 31 | +function prepareOnce(root: string, reported: string[] = []) { |
| 32 | + ensureEnginePrepared({ senpiRoot: root, omoVersion: OMO_VERSION, reinstallCommand: REINSTALL, report: (line: string) => reported.push(line) }) |
| 33 | +} |
| 34 | + |
| 35 | +const readUa = (root: string) => readFileSync(join(root, PI_AI_MESSAGES), "utf8") |
| 36 | +const readStamp = (root: string) => readFileSync(join(root, ENGINE_PREPARED_STAMP), "utf8").trim() |
| 37 | + |
| 38 | +afterEach(() => { |
| 39 | + for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true }) |
| 40 | +}) |
| 41 | + |
| 42 | +describe("launcher engine preparation (#8713)", () => { |
| 43 | + describe("#given an engine that postinstall never prepared", () => { |
| 44 | + test("#then the launch prepares it and stamps it with the omo version", () => { |
| 45 | + const root = createEngine("2.1.251") |
| 46 | + prepareOnce(root) |
| 47 | + expect(readUa(root)).toBe(uaSource("2.1.280")) |
| 48 | + expect(readFileSync(join(root, RPC_MODE), "utf8")).toContain("invalid_stream_event") |
| 49 | + expect(readStamp(root)).toBe(OMO_VERSION) |
| 50 | + }) |
| 51 | + }) |
| 52 | + |
| 53 | + describe("#given an engine already stamped for this omo version", () => { |
| 54 | + test("#then the launch leaves every engine file untouched", () => { |
| 55 | + const root = createEngine("2.1.251") |
| 56 | + prepareOnce(root) |
| 57 | + writeFileSync(join(root, PI_AI_MESSAGES), uaSource("2.1.100")) |
| 58 | + prepareOnce(root) |
| 59 | + expect(readUa(root)).toBe(uaSource("2.1.100")) |
| 60 | + }) |
| 61 | + }) |
| 62 | + |
| 63 | + describe("#given an engine stamped by a different omo version", () => { |
| 64 | + test("#then the launch prepares it again and restamps it", () => { |
| 65 | + const root = createEngine("2.1.251") |
| 66 | + writeFileSync(join(root, ENGINE_PREPARED_STAMP), "5.0.0-0.beta.85\n") |
| 67 | + prepareOnce(root) |
| 68 | + expect(readUa(root)).toBe(uaSource("2.1.280")) |
| 69 | + expect(readStamp(root)).toBe(OMO_VERSION) |
| 70 | + }) |
| 71 | + }) |
| 72 | + |
| 73 | + describe("#given an engine whose preparation fails", () => { |
| 74 | + test("#then the launch reports it with the reinstall command, does not throw, and leaves no stamp", () => { |
| 75 | + const root = createEngine("2.1.251") |
| 76 | + rmSync(join(root, RPC_MODE)) |
| 77 | + const reported: string[] = [] |
| 78 | + expect(() => prepareOnce(root, reported)).not.toThrow() |
| 79 | + expect(reported.join("")).toContain("rpc_patch_target_missing") |
| 80 | + expect(reported.join("")).toContain(REINSTALL) |
| 81 | + expect(existsSync(join(root, ENGINE_PREPARED_STAMP))).toBe(false) |
| 82 | + }) |
| 83 | + }) |
| 84 | +}) |
0 commit comments