-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathindex.test.ts
More file actions
156 lines (133 loc) · 6.49 KB
/
Copy pathindex.test.ts
File metadata and controls
156 lines (133 loc) · 6.49 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import { afterEach, beforeEach, describe, expect, it } from "bun:test";
import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { dirname, join, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { OhMyPiConnector } from "./src/index.js";
const originalEnv = {
HOME: process.env.HOME,
XDG_CONFIG_HOME: process.env.XDG_CONFIG_HOME,
PI_CODING_AGENT_DIR: process.env.PI_CODING_AGENT_DIR,
SIGNET_AGENT_ID: process.env.SIGNET_AGENT_ID,
SIGNET_DAEMON_URL: process.env.SIGNET_DAEMON_URL,
};
let tmpRoot = "";
function restoreEnv(name: keyof typeof originalEnv): void {
const value = originalEnv[name];
if (typeof value === "string") {
process.env[name] = value;
return;
}
delete process.env[name];
}
beforeEach(() => {
tmpRoot = mkdtempSync(join(tmpdir(), "signet-omp-connector-"));
process.env.HOME = tmpRoot;
process.env.XDG_CONFIG_HOME = join(tmpRoot, ".config-home");
process.env.PI_CODING_AGENT_DIR = join(tmpRoot, "agent");
process.env.SIGNET_AGENT_ID = "agent-from-env";
process.env.SIGNET_DAEMON_URL = "http://127.0.0.1:4123";
});
afterEach(() => {
restoreEnv("HOME");
restoreEnv("XDG_CONFIG_HOME");
restoreEnv("PI_CODING_AGENT_DIR");
restoreEnv("SIGNET_AGENT_ID");
restoreEnv("SIGNET_DAEMON_URL");
if (tmpRoot) {
rmSync(tmpRoot, { recursive: true, force: true });
}
});
describe("OhMyPiConnector", () => {
it("installs a bundled managed extension without external package resolution", async () => {
const connector = new OhMyPiConnector();
const result = await connector.install(tmpRoot);
const installedPath = join(tmpRoot, "agent", "extensions", "signet-oh-my-pi.js");
const configPath = join(tmpRoot, ".config-home", "signet", "oh-my-pi.json");
expect(result.success).toBe(true);
expect(result.filesWritten).toContain(installedPath);
expect(result.filesWritten).toContain(configPath);
expect(existsSync(installedPath)).toBe(true);
expect(existsSync(configPath)).toBe(true);
const content = readFileSync(installedPath, "utf8");
expect(content).toContain("SIGNET_MANAGED_OH_MY_PI_EXTENSION");
expect(content).toContain("Managed by Signet (@signet/oh-my-pi-extension)");
expect(content).toContain('Reflect.set(__signetRuntimeEnv, "SIGNET_AGENT_ID", "agent-from-env")');
expect(content).toContain('Reflect.set(__signetRuntimeEnv, "SIGNET_DAEMON_URL", "http://127.0.0.1:4123")');
expect(content).toContain(`Reflect.set(__signetRuntimeEnv, "SIGNET_PATH", ${JSON.stringify(tmpRoot)})`);
expect(content.length).toBeGreaterThan(1_000);
});
it("falls back to default agent id when none is configured at install time", async () => {
Reflect.deleteProperty(process.env, "SIGNET_AGENT_ID");
const connector = new OhMyPiConnector();
await connector.install(tmpRoot);
const installedPath = join(tmpRoot, "agent", "extensions", "signet-oh-my-pi.js");
const content = readFileSync(installedPath, "utf8");
expect(content).toContain('Reflect.set(__signetRuntimeEnv, "SIGNET_AGENT_ID", "default")');
});
it("treats blank runtime env values as missing when generating the managed bootstrap", async () => {
process.env.SIGNET_AGENT_ID = " ";
process.env.SIGNET_DAEMON_URL = "\n\t";
const connector = new OhMyPiConnector();
await connector.install(tmpRoot);
const installedPath = join(tmpRoot, "agent", "extensions", "signet-oh-my-pi.js");
const content = readFileSync(installedPath, "utf8");
expect(content).toContain('Reflect.set(__signetRuntimeEnv, "SIGNET_AGENT_ID", "default")');
expect(content).toContain('Reflect.set(__signetRuntimeEnv, "SIGNET_DAEMON_URL", "http://127.0.0.1:3850")');
});
it("persists a custom Oh My Pi agent dir so later maintenance still finds the managed install", async () => {
const customAgentDir = join(tmpRoot, "custom-agent-home");
process.env.PI_CODING_AGENT_DIR = customAgentDir;
const connector = new OhMyPiConnector();
await connector.install(tmpRoot);
Reflect.deleteProperty(process.env, "PI_CODING_AGENT_DIR");
const reloadedConnector = new OhMyPiConnector();
expect(reloadedConnector.isInstalled()).toBe(true);
const uninstall = await reloadedConnector.uninstall();
expect(uninstall.filesRemoved).toContain(join(customAgentDir, "extensions", "signet-oh-my-pi.js"));
expect(uninstall.filesRemoved).toContain(join(tmpRoot, ".config-home", "signet", "oh-my-pi.json"));
expect(reloadedConnector.isInstalled()).toBe(false);
});
});
describe("root build pipeline", () => {
it("builds Oh My Pi prerequisites before the shared dependency batch", () => {
const dir = dirname(fileURLToPath(import.meta.url));
const path = resolve(dir, "..", "..", "..", "package.json");
const text = readFileSync(path, "utf8");
expect(text).toContain('"build:oh-my-pi-extension"');
expect(text).toContain('"build:connector-oh-my-pi"');
const build = text.match(/"build":\s*"([^"]+)"/)?.[1] ?? "";
const ext = "bun run build:oh-my-pi-extension";
const connector = "bun run build:connector-oh-my-pi";
const deps = "bun run build:deps";
expect(build).toContain(ext);
expect(build).toContain(connector);
expect(build).toContain(deps);
expect(build.indexOf(ext)).toBeLessThan(build.indexOf(connector));
expect(build.indexOf(connector)).toBeLessThan(build.indexOf(deps));
});
it("keeps cli out of the parallel dependency batch", () => {
const dir = dirname(fileURLToPath(import.meta.url));
const path = resolve(dir, "..", "..", "..", "package.json");
const text = readFileSync(path, "utf8");
const deps = text.match(/"build:deps":\s*"([^"]+)"/)?.[1] ?? "";
expect(deps).not.toContain("@signet/cli");
expect(deps).not.toContain("@signet/connector-oh-my-pi");
});
});
describe("OhMyPiConnector.install — legacy SIGNET block migration", () => {
it("strips legacy block from AGENTS.md and reports path in filesWritten", async () => {
const agentsPath = join(tmpRoot, "AGENTS.md");
writeFileSync(agentsPath, "before\n<!-- SIGNET:START -->\nmanaged block\n<!-- SIGNET:END -->\nafter\n", "utf-8");
const result = await new OhMyPiConnector().install(tmpRoot);
expect(readFileSync(agentsPath, "utf-8")).toBe("before\nafter\n");
expect(result.filesWritten).toContain(agentsPath);
});
it("leaves AGENTS.md untouched when no legacy block present", async () => {
const agentsPath = join(tmpRoot, "AGENTS.md");
writeFileSync(agentsPath, "plain content\n", "utf-8");
const result = await new OhMyPiConnector().install(tmpRoot);
expect(readFileSync(agentsPath, "utf-8")).toBe("plain content\n");
expect(result.filesWritten).not.toContain(agentsPath);
});
});