forked from code-yeongyu/oh-my-openagent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostinstall.test.ts
More file actions
86 lines (71 loc) · 2.57 KB
/
Copy pathpostinstall.test.ts
File metadata and controls
86 lines (71 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/// <reference types="bun-types" />
import { describe, expect, test } from "bun:test"
import { spawnSync } from "node:child_process"
import { mkdtempSync, rmSync } from "node:fs"
import { tmpdir } from "node:os"
import { join } from "node:path"
import { fileURLToPath } from "node:url"
const postinstallPath = fileURLToPath(new URL("./postinstall.mjs", import.meta.url))
const RENAME_NOTICE =
"oh-my-openagent: the 'omo' command is now 'omo-agent-toolkit' (the old name was removed in this major release)."
type PostinstallRun = {
status: number | null
stdout: string
stderr: string
}
function runPostinstallInFixtureEnv(): PostinstallRun {
const fixtureHome = mkdtempSync(join(tmpdir(), "omo-postinstall-fixture-"))
try {
const result = spawnSync(process.execPath, [postinstallPath], {
encoding: "utf8",
env: {
...inheritedPlatformEnv(),
PATH: process.env.PATH ?? "",
HOME: fixtureHome,
USERPROFILE: fixtureHome,
XDG_CACHE_HOME: join(fixtureHome, "cache"),
},
})
return { status: result.status, stdout: result.stdout ?? "", stderr: result.stderr ?? "" }
} finally {
rmSync(fixtureHome, { recursive: true, force: true })
}
}
function inheritedPlatformEnv(): Record<string, string> {
const inherited: Record<string, string> = {}
for (const key of ["SystemRoot", "SYSTEMROOT", "ComSpec", "COMSPEC", "PATHEXT", "TEMP", "TMP", "windir"]) {
const value = process.env[key]
if (value !== undefined) inherited[key] = value
}
return inherited
}
function countNoticeLines(output: string): number {
return output.split("\n").filter((line) => line.trim() === RENAME_NOTICE).length
}
describe("postinstall rename notice", () => {
test("announces the omo-agent-toolkit rename exactly once", () => {
// #given
// a fixture environment isolated from the real HOME and OpenCode plugin cache
// #when
const run = runPostinstallInFixtureEnv()
// #then
expect(countNoticeLines(run.stdout)).toBe(1)
})
test("never fails the install regardless of platform binary resolution", () => {
// #given
// a fixture environment isolated from the real HOME and OpenCode plugin cache
// #when
const run = runPostinstallInFixtureEnv()
// #then
expect(run.status).toBe(0)
})
test("stays idempotent across repeated runs", () => {
// #given
const firstRun = runPostinstallInFixtureEnv()
// #when
const secondRun = runPostinstallInFixtureEnv()
// #then
expect(countNoticeLines(firstRun.stdout)).toBe(1)
expect(countNoticeLines(secondRun.stdout)).toBe(1)
})
})