Beautiful, AI-native markdown IDE and LLM wiki
TypeScript 2nd-brainagent-skillsclaudecodexcompany-braindocsknowledge-knowledge-baseknowledge-managementllmllm-wikillm-wiki-karpathymarkdownmarkdown-editormdmdxnotespkmsecond-brainskills
import { mkdtempSync } from 'node:fs';
import { mkdtemp, rm } from 'node:fs/promises';
import { createServer } from 'node:http';
import { tmpdir } from 'node:os';
import { resolve } from 'node:path';
import type { Hocuspocus } from '@hocuspocus/server';
import { afterAll, beforeAll, describe, expect, test } from 'vitest';
import type { BootedServer } from './boot.ts';
import {
bootCompositionRig,
parseProblem,
type RawResponse,
rawRequest,
} from './composition-rig.test-helper.ts';
import { listenOnLoopback } from './loopback-rig-test-helpers.ts';
import { mountMcpAndApi } from './mcp-mount.ts';
let tmpRoot: string;
let booted: BootedServer;
beforeAll(async () => {
tmpRoot = await mkdtemp(resolve(tmpdir(), 'ok-health-'));
booted = await bootCompositionRig(mkdtempSync(resolve(tmpRoot, 'proj-')));
}, 60_000);
afterAll(async () => {
await booted?.destroy();
await rm(tmpRoot, { recursive: true, force: true });
});
describe('/healthz', () => {
test('answers 200 ok with no-store, independent of readiness', async () => {
const res = await fetch(`http://127.0.0.1:${booted.port}/healthz`);
expect(res.status).toBe(200);
expect(await res.text()).toBe('ok');
expect(res.headers.get('cache-control')).toBe('no-store');
expect(res.headers.get('x-content-type-options')).toBe('nosniff');
});
test('is exempt from the Host gate (orchestrator probes send IP hosts)', async () => {
const res = await rawRequest(booted.port, '/healthz', {
headers: { Host: '10.0.0.7:8080' },
});
expect(res.status).toBe(200);
expect(res.body).toBe('ok');
});
test('is exempt from the forwarding-header tripwire (probes traverse proxies)', async () => {
const res = await rawRequest(booted.port, '/healthz', {
headers: { 'X-Forwarded-For': '203.0.113.7' },
});
expect(res.status).toBe(200);
});
test('non-GET is refused with 405 and an Allow header', async () => {
const res = await rawRequest(booted.port, '/healthz', { method: 'POST' });
expect(res.status).toBe(405);
expect(res.headers.allow).toBe('GET, HEAD');
expect(parseProblem(res.body).type).toBe('urn:ok:error:method-not-allowed');
});
test('query strings do not break the exact match', async () => {
const res = await fetch(`http://127.0.0.1:${booted.port}/healthz?probe=1`);
expect(res.status).toBe(200);
});
});
describe('/readyz', () => {
test('reports 200 with the single-shaped body once init settles', async () => {
await booted.ready;
const res = await fetch(`http://127.0.0.1:${booted.port}/readyz`);
expect(res.status).toBe(200);
const body = (await res.json()) as { ready: boolean; status: string; degraded: string[] };
expect(body.ready).toBe(true);
expect(body.status).toBe('ready');
expect(Array.isArray(body.degraded)).toBe(true);
});
test('during boot answers either 503 not-ready or 200 ready, never a gate refusal', async () => {
const tmp = await mkdtemp(resolve(tmpdir(), 'ok-readyz-race-'));
try {
const racing = await bootCompositionRig(tmp);
try {
const res = await fetch(`http://127.0.0.1:${racing.port}/readyz`);
expect([200, 503]).toContain(res.status);
const body = (await res.json()) as { ready: boolean };
expect(body.ready).toBe(res.status === 200);
await racing.ready;
const settled = await fetch(`http://127.0.0.1:${racing.port}/readyz`);
expect(settled.status).toBe(200);
} finally {
await racing.destroy();
}
} finally {
await rm(tmp, { recursive: true, force: true });
}
}, 30_000);
test('destroy() flips readiness to draining before the listener closes', async () => {
const tmp = await mkdtemp(resolve(tmpdir(), 'ok-readyz-drain-'));
try {
const draining = await bootCompositionRig(tmp);
try {
await draining.ready;
const { httpServer } = draining;
const closeListener = httpServer.close.bind(httpServer);
let probeBeforeClose: Promise<RawResponse> | undefined;
httpServer.close = ((callback?: (err?: Error) => void) => {
probeBeforeClose = rawRequest(draining.port, '/readyz');
const close = () => {
closeListener(callback);
};
void probeBeforeClose.then(close, close);
return httpServer;
}) as typeof httpServer.close;
await draining.destroy();
expect(probeBeforeClose).toBeDefined();
const probe = await probeBeforeClose;
expect(probe?.status).toBe(503);
expect(JSON.parse(probe?.body ?? '')).toMatchObject({ ready: false, status: 'draining' });
} finally {
await draining.destroy();
}
} finally {
await rm(tmp, { recursive: true, force: true });
}
}, 30_000);
test('is exempt from the Host gate like /healthz', async () => {
const res = await rawRequest(booted.port, '/readyz', {
headers: { Host: 'kb.internal:8080' },
});
expect(res.status).toBe(200);
});
});
describe('/readyz provider states (mounted harness)', () => {
const fakeLog = {
error: () => {},
warn: () => {},
info: () => {},
debug: () => {},
child: () => fakeLog,
};
const fakeHocuspocus = {
hooks: async () => {},
handleConnection: () => ({ handleMessage: () => {}, handleClose: () => {} }),
} as unknown as Hocuspocus;
async function probeWithReadiness(
readiness: 'ready' | 'failed' | 'draining' | undefined,
): Promise<{ status: number; body: { ready: boolean; status: string; degraded: string[] } }> {
const httpServer = createServer();
const mount = mountMcpAndApi({
httpServer,
nativeApi: undefined,
hocuspocus: fakeHocuspocus,
log: fakeLog as never,
...(readiness === undefined
? {}
: { health: { readiness: () => readiness, degraded: () => ['shadow-repo'] } }),
});
const { port } = await listenOnLoopback(httpServer);
try {
const res = await fetch(`http://127.0.0.1:${port}/readyz`);
return {
status: res.status,
body: (await res.json()) as { ready: boolean; status: string; degraded: string[] },
};
} finally {
await mount.shutdown();
await new Promise<void>((r) => mount.wss.close(() => r()));
await new Promise<void>((r) => httpServer.close(() => r()));
}
}
test('failed init reports 503 with status failed and an empty degraded list', async () => {
const probe = await probeWithReadiness('failed');
expect(probe.status).toBe(503);
expect(probe.body).toEqual({ ready: false, status: 'failed', degraded: [] });
});
test('draining reports 503 with status draining', async () => {
const probe = await probeWithReadiness('draining');
expect(probe.status).toBe(503);
expect(probe.body).toEqual({ ready: false, status: 'draining', degraded: [] });
});
test('ready passes the degraded list through into the 200 body', async () => {
const probe = await probeWithReadiness('ready');
expect(probe.status).toBe(200);
expect(probe.body).toEqual({ ready: true, status: 'ready', degraded: ['shadow-repo'] });
});
test('a throwing provider yields a structured 500, not the router default', async () => {
const httpServer = createServer();
const mount = mountMcpAndApi({
httpServer,
nativeApi: undefined,
hocuspocus: fakeHocuspocus,
log: fakeLog as never,
health: {
readiness: () => {
throw new Error('provider exploded');
},
degraded: () => [],
},
});
const { port } = await listenOnLoopback(httpServer);
try {
const res = await fetch(`http://127.0.0.1:${port}/readyz`);
expect(res.status).toBe(500);
expect(res.headers.get('content-type')).toBe('application/problem+json');
const body = (await res.json()) as { type: string };
expect(body.type).toBe('urn:ok:error:internal-server-error');
} finally {
await mount.shutdown();
await new Promise<void>((r) => mount.wss.close(() => r()));
await new Promise<void>((r) => httpServer.close(() => r()));
}
});
test('an omitted provider reports ready (synchronous-init harness posture)', async () => {
const probe = await probeWithReadiness(undefined);
expect(probe.status).toBe(200);
expect(probe.body).toEqual({ ready: true, status: 'ready', degraded: [] });
});
});