0ef5fcb1c5
Security / Dependency audit (pip-audit) (push) Has been cancelled
Security / CodeQL (javascript-typescript) (push) Has been cancelled
Security / CodeQL (python) (push) Has been cancelled
Security / Secret scan (gitleaks) (push) Has been cancelled
rust / test (ubuntu) (push) Has been cancelled
rust / simulator e2e (macos-latest) (push) Has been cancelled
rust / simulator e2e (ubuntu-latest) (push) Has been cancelled
rust / simulator e2e (windows-latest) (push) Has been cancelled
rust / wheels (aarch64-apple-darwin) (push) Has been cancelled
rust / wheels (x86_64-unknown-linux-gnu) (push) Has been cancelled
rust / wheels (x86_64-apple-darwin) (push) Has been cancelled
rust / audit (push) Has been cancelled
rust / parity (nightly, allowed to fail during Phase 0) (push) Has been cancelled
CI / commitlint (push) Has been skipped
Dev Containers / validate (.devcontainer/devcontainer.json, default) (push) Failing after 0s
Dev Containers / validate (.devcontainer/memory-stack/devcontainer.json, memory-stack) (push) Failing after 0s
Dev Containers / validate-worktree (push) Failing after 0s
CI / changes (push) Failing after 4s
Deploy Documentation / validate (push) Has been skipped
Deploy Documentation / deploy (push) Failing after 1s
Init Native E2E / init-native (ubuntu-latest, claude) (push) Failing after 1s
Init Native E2E / init-native (ubuntu-latest, codex) (push) Failing after 1s
Install Native E2E / install-native (ubuntu-latest) (push) Failing after 1s
OpenCode Plugin / typecheck + build + test (push) Failing after 1s
Init Native E2E / init-native (ubuntu-latest, copilot) (push) Failing after 1s
Release Please / release-please (push) Failing after 1s
Wrap E2E / docker-wrap-e2e (push) Failing after 1s
Wrap Native E2E / wrap-native (ubuntu-latest) (push) Failing after 1s
Init E2E / docker-init-e2e (push) Failing after 4s
Merge Conflicts / merge-conflicts (push) Failing after 4s
CI / lint (push) Has been cancelled
CI / build-wheel (push) Has been cancelled
CI / build-wheel-windows (push) Has been cancelled
CI / prefetch-model (push) Has been cancelled
CI / test-dashboard-ui (push) Has been cancelled
CI / test (1) (push) Has been cancelled
CI / test (2) (push) Has been cancelled
CI / test (3) (push) Has been cancelled
CI / test (4) (push) Has been cancelled
CI / test-extras (push) Has been cancelled
CI / test-agno (push) Has been cancelled
CI / build (push) Has been cancelled
CI / workflow-validation (push) Has been cancelled
CI / docker-native-e2e (push) Has been cancelled
CI / windows-native-wrapper (push) Has been cancelled
CI / macos-native-wrapper (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code-nonroot name:code-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code-slim name:code-slim]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code-slim-nonroot name:code-slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-nonroot name:nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-slim name:slim]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-slim-nonroot name:slim-nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime name:]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code name:code]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code-nonroot name:code-nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code-slim name:code-slim]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code-slim-nonroot name:code-slim-nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-nonroot name:nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-slim name:slim]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-slim-nonroot name:slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime name:]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code name:code]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code-nonroot name:code-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code-slim name:code-slim]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code-slim-nonroot name:code-slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-nonroot name:nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-slim name:slim]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-slim-nonroot name:slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime name:]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code name:code]) (push) Has been cancelled
Docker / promote-latest (push) Has been cancelled
Init Native E2E / init-native (macos-latest, claude) (push) Has been cancelled
Init Native E2E / init-native (macos-latest, codex) (push) Has been cancelled
Init Native E2E / init-native (macos-latest, copilot) (push) Has been cancelled
Install Native E2E / install-native (macos-latest) (push) Has been cancelled
Wrap Native E2E / wrap-native (macos-latest) (push) Has been cancelled
200 lines
6.3 KiB
TypeScript
200 lines
6.3 KiB
TypeScript
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
|
|
const mocked = vi.hoisted(() => ({
|
|
start: vi.fn(async () => "http://127.0.0.1:8787"),
|
|
stop: vi.fn(async () => undefined),
|
|
logger: {
|
|
debug: vi.fn(),
|
|
error: vi.fn(),
|
|
info: vi.fn(),
|
|
warn: vi.fn(),
|
|
},
|
|
}));
|
|
|
|
vi.mock("headroom-ai", () => ({
|
|
compress: vi.fn(),
|
|
}));
|
|
|
|
vi.mock("../src/proxy-manager.js", () => ({
|
|
ProxyManager: class {
|
|
start = mocked.start;
|
|
stop = mocked.stop;
|
|
},
|
|
defaultLogger: mocked.logger,
|
|
}));
|
|
|
|
import { HeadroomContextEngine } from "../src/engine.js";
|
|
|
|
afterEach(() => {
|
|
mocked.start.mockReset();
|
|
mocked.start.mockResolvedValue("http://127.0.0.1:8787");
|
|
mocked.stop.mockClear();
|
|
mocked.logger.debug.mockClear();
|
|
mocked.logger.error.mockClear();
|
|
mocked.logger.info.mockClear();
|
|
mocked.logger.warn.mockClear();
|
|
});
|
|
|
|
describe("HeadroomContextEngine proxy startup helpers", () => {
|
|
it("bootstraps by scheduling proxy startup when enabled", async () => {
|
|
const engine = new HeadroomContextEngine();
|
|
|
|
await expect(
|
|
engine.bootstrap({
|
|
sessionId: "session-1",
|
|
sessionFile: "session.jsonl",
|
|
}),
|
|
).resolves.toEqual({
|
|
bootstrapped: true,
|
|
reason: "proxy startup scheduled",
|
|
});
|
|
expect(mocked.start).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("removes unsubscribed proxy listeners before notifying readiness", async () => {
|
|
const engine = new HeadroomContextEngine();
|
|
const first = vi.fn();
|
|
const second = vi.fn();
|
|
|
|
const unsubscribeFirst = engine.onProxyReady(first);
|
|
engine.onProxyReady(second);
|
|
unsubscribeFirst();
|
|
|
|
engine.ensureProxyStarted();
|
|
await engine.ensureProxyUrl();
|
|
|
|
expect(first).not.toHaveBeenCalled();
|
|
expect(second).toHaveBeenCalledWith("http://127.0.0.1:8787");
|
|
});
|
|
|
|
it("returns the existing proxy URL without starting again", async () => {
|
|
const engine = new HeadroomContextEngine();
|
|
|
|
(engine as { proxyUrl: string | null }).proxyUrl = "http://127.0.0.1:8787";
|
|
|
|
await expect(engine.ensureProxyUrl()).resolves.toBe("http://127.0.0.1:8787");
|
|
expect(mocked.start).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("throws when proxy startup is disabled", async () => {
|
|
const engine = new HeadroomContextEngine({ enabled: false });
|
|
|
|
await expect(engine.ensureProxyUrl()).rejects.toThrow("Headroom proxy startup is disabled");
|
|
expect(mocked.start).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("does not emit an unhandledRejection when fire-and-forget startup fails", async () => {
|
|
mocked.start.mockReset();
|
|
mocked.start.mockRejectedValue(new Error("proxy boom"));
|
|
|
|
const engine = new HeadroomContextEngine();
|
|
const unhandled: unknown[] = [];
|
|
const onUnhandled = (reason: unknown) => unhandled.push(reason);
|
|
process.on("unhandledRejection", onUnhandled);
|
|
|
|
try {
|
|
// Fire-and-forget: caller intentionally does not await.
|
|
engine.ensureProxyStarted();
|
|
// Let the startup promise settle and any microtasks/macrotasks flush.
|
|
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
|
|
expect(unhandled).toEqual([]);
|
|
expect(mocked.logger.warn).toHaveBeenCalledWith(
|
|
expect.stringContaining("Headroom proxy unavailable"),
|
|
);
|
|
} finally {
|
|
process.off("unhandledRejection", onUnhandled);
|
|
}
|
|
});
|
|
|
|
it("stores the startup failure in getProxyStartupError()", async () => {
|
|
const failure = new Error("proxy boom");
|
|
mocked.start.mockReset();
|
|
mocked.start.mockRejectedValue(failure);
|
|
|
|
const engine = new HeadroomContextEngine();
|
|
expect(engine.getProxyStartupError()).toBeNull();
|
|
|
|
engine.ensureProxyStarted();
|
|
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
|
|
expect(engine.getProxyStartupError()).toBe(failure);
|
|
});
|
|
|
|
it("allows retrying startup after a failure", async () => {
|
|
mocked.start.mockReset();
|
|
mocked.start
|
|
.mockRejectedValueOnce(new Error("proxy boom"))
|
|
.mockResolvedValueOnce("http://127.0.0.1:8787");
|
|
|
|
const engine = new HeadroomContextEngine();
|
|
|
|
engine.ensureProxyStarted();
|
|
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
expect(engine.getProxyStartupError()).toBeInstanceOf(Error);
|
|
|
|
// A second attempt is possible once the failed promise has cleared.
|
|
const url = await engine.ensureProxyUrl();
|
|
expect(url).toBe("http://127.0.0.1:8787");
|
|
expect(engine.getProxyStartupError()).toBeNull();
|
|
expect(mocked.start).toHaveBeenCalledTimes(2);
|
|
});
|
|
|
|
it("ensureProxyUrl rejects cleanly on startup failure without unhandledRejection", async () => {
|
|
const failure = new Error("proxy boom");
|
|
mocked.start.mockReset();
|
|
mocked.start.mockRejectedValue(failure);
|
|
|
|
const engine = new HeadroomContextEngine();
|
|
const unhandled: unknown[] = [];
|
|
const onUnhandled = (reason: unknown) => unhandled.push(reason);
|
|
process.on("unhandledRejection", onUnhandled);
|
|
|
|
try {
|
|
await expect(engine.ensureProxyUrl()).rejects.toBe(failure);
|
|
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
expect(unhandled).toEqual([]);
|
|
} finally {
|
|
process.off("unhandledRejection", onUnhandled);
|
|
}
|
|
});
|
|
|
|
it("isolates and logs proxy-ready listener rejections", async () => {
|
|
const engine = new HeadroomContextEngine();
|
|
const failing = vi.fn(async () => {
|
|
throw new Error("listener boom");
|
|
});
|
|
const healthy = vi.fn();
|
|
|
|
engine.onProxyReady(failing);
|
|
engine.onProxyReady(healthy);
|
|
|
|
engine.ensureProxyStarted();
|
|
// ensureProxyUrl must still resolve despite the listener throwing.
|
|
await expect(engine.ensureProxyUrl()).resolves.toBe("http://127.0.0.1:8787");
|
|
|
|
expect(failing).toHaveBeenCalled();
|
|
expect(healthy).toHaveBeenCalledWith("http://127.0.0.1:8787");
|
|
expect(mocked.logger.warn).toHaveBeenCalledWith(
|
|
expect.stringContaining("Headroom proxy ready listener failed"),
|
|
);
|
|
expect(engine.getProxyStartupError()).toBeNull();
|
|
});
|
|
|
|
it("schedules startup and returns original messages when assembling before proxy readiness", async () => {
|
|
const engine = new HeadroomContextEngine();
|
|
const messages = [{ role: "user", content: "hello" }];
|
|
|
|
await expect(
|
|
engine.assemble({
|
|
sessionId: "session-1",
|
|
messages,
|
|
}),
|
|
).resolves.toEqual({
|
|
messages,
|
|
estimatedTokens: 0,
|
|
});
|
|
expect(mocked.start).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|