|
1 | 1 | import { EventEmitter } from "node:events"; |
2 | | -import { expect, test } from "bun:test"; |
| 2 | +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
| 5 | +import { afterEach, expect, test } from "bun:test"; |
3 | 6 |
|
4 | | -import { main } from "../index.js"; |
5 | | -import { clear, snapshot } from "./index.js"; |
| 7 | +import { main, mainWithRunners } from "../index.js"; |
| 8 | +import { |
| 9 | + resetPricingMetadataRefreshForTests, |
| 10 | + schedulePricingMetadataRefresh, |
| 11 | +} from "../cost/pricing-metadata.js"; |
| 12 | +import { clear, snapshot, type PerfSpan } from "./index.js"; |
6 | 13 | import { endBootSpans, startBootSpans } from "./boot-spans.js"; |
7 | 14 | import { createHarness } from "../tui/harness.js"; |
8 | 15 | import { mountRunnerHost } from "../tui/runner/host.js"; |
9 | 16 |
|
| 17 | +function bootPair(spans: readonly PerfSpan[]): PerfSpan[] { |
| 18 | + return spans.filter( |
| 19 | + (span) => span.name === "boot" || span.name === "first-paint", |
| 20 | + ); |
| 21 | +} |
| 22 | + |
10 | 23 | test("boot starts boot and first-paint spans before config exits", async () => { |
11 | 24 | clear(); |
12 | 25 | endBootSpans(); |
@@ -48,3 +61,85 @@ test("mount ends the boot and first-paint spans", async () => { |
48 | 61 | clear(); |
49 | 62 | } |
50 | 63 | }); |
| 64 | + |
| 65 | +let sandbox: string | undefined; |
| 66 | + |
| 67 | +afterEach(() => { |
| 68 | + if (sandbox !== undefined) { |
| 69 | + rmSync(sandbox, { recursive: true, force: true }); |
| 70 | + sandbox = undefined; |
| 71 | + } |
| 72 | +}); |
| 73 | + |
| 74 | +function sandboxArgs( |
| 75 | + subcommand: readonly string[], |
| 76 | + rest: readonly string[] = [], |
| 77 | +): string[] { |
| 78 | + const root = mkdtempSync(join(tmpdir(), "boot-span-")); |
| 79 | + sandbox = root; |
| 80 | + const settingsDir = join(root, "home", ".corbits"); |
| 81 | + mkdirSync(settingsDir, { recursive: true }); |
| 82 | + writeFileSync( |
| 83 | + join(settingsDir, "settings.json"), |
| 84 | + JSON.stringify({ |
| 85 | + providers: { |
| 86 | + "test-provider": { |
| 87 | + baseURL: "http://localhost:1234", |
| 88 | + apiKey: "test-key", |
| 89 | + models: ["test-model"], |
| 90 | + defaultModel: "test-model", |
| 91 | + }, |
| 92 | + }, |
| 93 | + defaultProvider: "test-provider", |
| 94 | + }), |
| 95 | + ); |
| 96 | + mkdirSync(join(root, "project"), { recursive: true }); |
| 97 | + resetPricingMetadataRefreshForTests(); |
| 98 | + schedulePricingMetadataRefresh({ |
| 99 | + cachePath: join(settingsDir, "cache", "models-pricing.json"), |
| 100 | + fetchImpl: () => Promise.reject(new Error("network disabled in tests")), |
| 101 | + }); |
| 102 | + return [ |
| 103 | + ...subcommand, |
| 104 | + "--cwd", |
| 105 | + join(root, "project"), |
| 106 | + "--config", |
| 107 | + join(settingsDir, "settings.json"), |
| 108 | + ...rest, |
| 109 | + ]; |
| 110 | +} |
| 111 | + |
| 112 | +test("exec closes boot spans before returning, without a TUI mount", async () => { |
| 113 | + clear(); |
| 114 | + endBootSpans(); |
| 115 | + const code = await mainWithRunners(sandboxArgs(["exec"], ["say hello"]), { |
| 116 | + runTUI: async () => 0, |
| 117 | + runExec: async () => 0, |
| 118 | + runOnboarding: async () => 0, |
| 119 | + }); |
| 120 | + expect(code).toBe(0); |
| 121 | + const pair = bootPair(snapshot()); |
| 122 | + expect(pair).toHaveLength(2); |
| 123 | + expect(pair.every((span) => span.endNs !== undefined)).toBe(true); |
| 124 | + clear(); |
| 125 | +}); |
| 126 | + |
| 127 | +test("a TUI that already closed boot spans is not ended twice", async () => { |
| 128 | + clear(); |
| 129 | + endBootSpans(); |
| 130 | + let closedAt: bigint | undefined; |
| 131 | + const code = await mainWithRunners(sandboxArgs([]), { |
| 132 | + runTUI: async () => { |
| 133 | + endBootSpans(); |
| 134 | + closedAt = snapshot().find((span) => span.name === "boot")?.endNs; |
| 135 | + return 0; |
| 136 | + }, |
| 137 | + runExec: async () => 0, |
| 138 | + runOnboarding: async () => 0, |
| 139 | + }); |
| 140 | + expect(code).toBe(0); |
| 141 | + const boot = snapshot().find((span) => span.name === "boot"); |
| 142 | + expect(boot?.endNs).toBe(closedAt); |
| 143 | + expect(bootPair(snapshot())).toHaveLength(2); |
| 144 | + clear(); |
| 145 | +}); |
0 commit comments