import { test } from 'node:test' import assert from 'node:assert/strict' import fs from 'node:fs' import os from 'node:os' import path from 'node:path' import { startServer } from '../src/server.mjs' function tmpProject() { return fs.mkdtempSync(path.join(os.tmpdir(), 'kilo-server-')) } function withTimeout(promise, ms, message) { return Promise.race([ promise, new Promise((_, reject) => { const t = setTimeout(() => reject(new Error(message)), ms) t.unref?.() }), ]) } test('close resolves while a dashboard SSE client stays connected', async () => { const project = tmpProject() const started = await startServer({ project, port: 0, host: '127.0.0.1' }, null) const url = `http://127.0.0.1:${started.server.address().port}` const ac = new AbortController() const res = await fetch(`${url}/api/events`, { signal: ac.signal }) const reader = res.body.getReader() await reader.read() // ': connected' — the stream stays open now await withTimeout(started.close(), 2000, 'close() hung with an open SSE client') ac.abort() }) test('close is idempotent', async () => { const project = tmpProject() const started = await startServer({ project, port: 0, host: '127.0.0.1' }, null) await started.close() await started.close() })