|
| 1 | +import { afterEach, beforeEach, describe, expect, it, spyOn } from "bun:test"; |
| 2 | +import { mkdirSync, writeFileSync } from "node:fs"; |
| 3 | +import { join } from "node:path"; |
| 4 | +import { cmdPullHistory, parseAndMergeChildHistory } from "../commands/pull-history.js"; |
| 5 | +import * as historyModule from "../history.js"; |
| 6 | +import { loadHistory } from "../history.js"; |
| 7 | + |
| 8 | +// ─── parseAndMergeChildHistory tests ───────────────────────────────────────── |
| 9 | + |
| 10 | +describe("parseAndMergeChildHistory", () => { |
| 11 | + let origSpawnHome: string | undefined; |
| 12 | + |
| 13 | + beforeEach(() => { |
| 14 | + origSpawnHome = process.env.SPAWN_HOME; |
| 15 | + // Use isolated temp dir for history (preload sets HOME to a temp dir) |
| 16 | + const tmpHome = process.env.HOME ?? "/tmp"; |
| 17 | + const spawnDir = join(tmpHome, `.spawn-test-${Date.now()}-${Math.random()}`); |
| 18 | + mkdirSync(spawnDir, { |
| 19 | + recursive: true, |
| 20 | + }); |
| 21 | + process.env.SPAWN_HOME = spawnDir; |
| 22 | + // Write empty history |
| 23 | + writeFileSync( |
| 24 | + join(spawnDir, "history.json"), |
| 25 | + JSON.stringify({ |
| 26 | + version: 1, |
| 27 | + records: [], |
| 28 | + }), |
| 29 | + ); |
| 30 | + }); |
| 31 | + |
| 32 | + afterEach(() => { |
| 33 | + if (origSpawnHome === undefined) { |
| 34 | + delete process.env.SPAWN_HOME; |
| 35 | + } else { |
| 36 | + process.env.SPAWN_HOME = origSpawnHome; |
| 37 | + } |
| 38 | + }); |
| 39 | + |
| 40 | + it("returns 0 for empty string", () => { |
| 41 | + expect(parseAndMergeChildHistory("", "parent-123")).toBe(0); |
| 42 | + }); |
| 43 | + |
| 44 | + it("returns 0 for empty object", () => { |
| 45 | + expect(parseAndMergeChildHistory("{}", "parent-123")).toBe(0); |
| 46 | + }); |
| 47 | + |
| 48 | + it("returns 0 for invalid JSON", () => { |
| 49 | + expect(parseAndMergeChildHistory("not json", "parent-123")).toBe(0); |
| 50 | + }); |
| 51 | + |
| 52 | + it("returns 0 for empty records array", () => { |
| 53 | + const json = JSON.stringify({ |
| 54 | + version: 1, |
| 55 | + records: [], |
| 56 | + }); |
| 57 | + expect(parseAndMergeChildHistory(json, "parent-123")).toBe(0); |
| 58 | + }); |
| 59 | + |
| 60 | + it("parses and merges valid child records", () => { |
| 61 | + const json = JSON.stringify({ |
| 62 | + version: 1, |
| 63 | + records: [ |
| 64 | + { |
| 65 | + id: "child-1", |
| 66 | + agent: "claude", |
| 67 | + cloud: "hetzner", |
| 68 | + timestamp: "2026-03-26T00:00:00Z", |
| 69 | + }, |
| 70 | + { |
| 71 | + id: "child-2", |
| 72 | + agent: "codex", |
| 73 | + cloud: "digitalocean", |
| 74 | + timestamp: "2026-03-26T00:01:00Z", |
| 75 | + name: "test-spawn", |
| 76 | + }, |
| 77 | + ], |
| 78 | + }); |
| 79 | + |
| 80 | + const count = parseAndMergeChildHistory(json, "parent-123"); |
| 81 | + expect(count).toBe(2); |
| 82 | + |
| 83 | + // Verify records were merged into history |
| 84 | + const history = loadHistory(); |
| 85 | + const child1 = history.find((r) => r.id === "child-1"); |
| 86 | + const child2 = history.find((r) => r.id === "child-2"); |
| 87 | + expect(child1).toBeDefined(); |
| 88 | + expect(child1!.agent).toBe("claude"); |
| 89 | + expect(child1!.parent_id).toBe("parent-123"); |
| 90 | + expect(child2).toBeDefined(); |
| 91 | + expect(child2!.name).toBe("test-spawn"); |
| 92 | + expect(child2!.parent_id).toBe("parent-123"); |
| 93 | + }); |
| 94 | + |
| 95 | + it("preserves existing parent_id from child records", () => { |
| 96 | + const json = JSON.stringify({ |
| 97 | + version: 1, |
| 98 | + records: [ |
| 99 | + { |
| 100 | + id: "grandchild-1", |
| 101 | + agent: "claude", |
| 102 | + cloud: "aws", |
| 103 | + timestamp: "2026-03-26T00:00:00Z", |
| 104 | + parent_id: "child-abc", |
| 105 | + depth: 2, |
| 106 | + }, |
| 107 | + ], |
| 108 | + }); |
| 109 | + |
| 110 | + const count = parseAndMergeChildHistory(json, "parent-123"); |
| 111 | + expect(count).toBe(1); |
| 112 | + |
| 113 | + const history = loadHistory(); |
| 114 | + const gc = history.find((r) => r.id === "grandchild-1"); |
| 115 | + expect(gc).toBeDefined(); |
| 116 | + // parent_id should be preserved from the child record, not overwritten |
| 117 | + // (mergeChildHistory only sets parent_id if it's not already set) |
| 118 | + expect(gc!.parent_id).toBe("child-abc"); |
| 119 | + expect(gc!.depth).toBe(2); |
| 120 | + }); |
| 121 | + |
| 122 | + it("skips records without an id", () => { |
| 123 | + const json = JSON.stringify({ |
| 124 | + version: 1, |
| 125 | + records: [ |
| 126 | + { |
| 127 | + agent: "claude", |
| 128 | + cloud: "hetzner", |
| 129 | + timestamp: "2026-03-26T00:00:00Z", |
| 130 | + }, |
| 131 | + { |
| 132 | + id: "valid-1", |
| 133 | + agent: "codex", |
| 134 | + cloud: "gcp", |
| 135 | + timestamp: "2026-03-26T00:01:00Z", |
| 136 | + }, |
| 137 | + ], |
| 138 | + }); |
| 139 | + |
| 140 | + const count = parseAndMergeChildHistory(json, "parent-123"); |
| 141 | + expect(count).toBe(1); |
| 142 | + }); |
| 143 | + |
| 144 | + it("preserves connection info from child records", () => { |
| 145 | + const json = JSON.stringify({ |
| 146 | + version: 1, |
| 147 | + records: [ |
| 148 | + { |
| 149 | + id: "child-conn", |
| 150 | + agent: "claude", |
| 151 | + cloud: "digitalocean", |
| 152 | + timestamp: "2026-03-26T00:00:00Z", |
| 153 | + connection: { |
| 154 | + ip: "10.0.0.1", |
| 155 | + user: "root", |
| 156 | + server_id: "12345", |
| 157 | + }, |
| 158 | + }, |
| 159 | + ], |
| 160 | + }); |
| 161 | + |
| 162 | + const count = parseAndMergeChildHistory(json, "parent-123"); |
| 163 | + expect(count).toBe(1); |
| 164 | + |
| 165 | + const history = loadHistory(); |
| 166 | + const child = history.find((r) => r.id === "child-conn"); |
| 167 | + expect(child!.connection?.ip).toBe("10.0.0.1"); |
| 168 | + expect(child!.connection?.server_id).toBe("12345"); |
| 169 | + }); |
| 170 | + |
| 171 | + it("deduplicates — calling twice with same records only merges once", () => { |
| 172 | + const json = JSON.stringify({ |
| 173 | + version: 1, |
| 174 | + records: [ |
| 175 | + { |
| 176 | + id: "dedup-1", |
| 177 | + agent: "claude", |
| 178 | + cloud: "hetzner", |
| 179 | + timestamp: "2026-03-26T00:00:00Z", |
| 180 | + }, |
| 181 | + ], |
| 182 | + }); |
| 183 | + |
| 184 | + parseAndMergeChildHistory(json, "parent-123"); |
| 185 | + parseAndMergeChildHistory(json, "parent-123"); |
| 186 | + |
| 187 | + const history = loadHistory(); |
| 188 | + const matches = history.filter((r) => r.id === "dedup-1"); |
| 189 | + expect(matches.length).toBe(1); |
| 190 | + }); |
| 191 | + |
| 192 | + it("handles whitespace-only input", () => { |
| 193 | + expect(parseAndMergeChildHistory(" \n ", "parent-123")).toBe(0); |
| 194 | + }); |
| 195 | + |
| 196 | + it("handles history without version field", () => { |
| 197 | + const json = JSON.stringify({ |
| 198 | + records: [ |
| 199 | + { |
| 200 | + id: "no-version", |
| 201 | + agent: "hermes", |
| 202 | + cloud: "sprite", |
| 203 | + timestamp: "2026-03-26T00:00:00Z", |
| 204 | + }, |
| 205 | + ], |
| 206 | + }); |
| 207 | + |
| 208 | + const count = parseAndMergeChildHistory(json, "parent-123"); |
| 209 | + expect(count).toBe(1); |
| 210 | + }); |
| 211 | +}); |
| 212 | + |
| 213 | +// ─── cmdPullHistory tests ─────────────────────────────────────────────────── |
| 214 | + |
| 215 | +describe("cmdPullHistory", () => { |
| 216 | + it("returns immediately when no active servers", async () => { |
| 217 | + const spy = spyOn(historyModule, "getActiveServers").mockReturnValue([]); |
| 218 | + await cmdPullHistory(); |
| 219 | + expect(spy).toHaveBeenCalledTimes(1); |
| 220 | + spy.mockRestore(); |
| 221 | + }); |
| 222 | + |
| 223 | + it("skips servers without connection info", async () => { |
| 224 | + const spy = spyOn(historyModule, "getActiveServers").mockReturnValue([ |
| 225 | + { |
| 226 | + id: "test-1", |
| 227 | + agent: "claude", |
| 228 | + cloud: "hetzner", |
| 229 | + timestamp: "2026-03-26T00:00:00Z", |
| 230 | + }, |
| 231 | + ]); |
| 232 | + // Should not throw — just skips the record with no connection |
| 233 | + await cmdPullHistory(); |
| 234 | + spy.mockRestore(); |
| 235 | + }); |
| 236 | + |
| 237 | + it("skips servers with missing ip", async () => { |
| 238 | + const spy = spyOn(historyModule, "getActiveServers").mockReturnValue([ |
| 239 | + { |
| 240 | + id: "test-2", |
| 241 | + agent: "claude", |
| 242 | + cloud: "hetzner", |
| 243 | + timestamp: "2026-03-26T00:00:00Z", |
| 244 | + connection: { |
| 245 | + ip: "", |
| 246 | + user: "root", |
| 247 | + }, |
| 248 | + }, |
| 249 | + ]); |
| 250 | + await cmdPullHistory(); |
| 251 | + spy.mockRestore(); |
| 252 | + }); |
| 253 | +}); |
0 commit comments