- pnpm workspace: apps/{web,server}, packages/{contracts,db,domain,adapters,tasks,agent,testkit}
- server: Hono + zod-openapi, API Key auth with scopes, redacting pino logger, SSE event bus with replay, worker heartbeat
- db: Kysely + pg, file migrator, 0001_init (settings/api_key/audit_event/outbox_event)
- web: React Router 7 SPA console shell with 12 workspace placeholders, uhd_web tokens/ui/guard scripts
- docs: ADR-001 (Node over Deno), dev runbook, design system rules
Co-Authored-By: Claude Fable 5.1 <[email protected]>
50 lines
1.6 KiB
TypeScript
50 lines
1.6 KiB
TypeScript
import { Writable } from "node:stream";
|
|
import { createApp } from "../src/http/app";
|
|
import { type ApiKeyStore, hashApiKey } from "../src/http/auth";
|
|
import { EventBus } from "../src/http/sse";
|
|
import { createLogger } from "../src/logger";
|
|
|
|
export const DEV_KEY = "dev-admin-test-key";
|
|
|
|
/** 收集日志行 */
|
|
export function memoryLogger() {
|
|
const lines: string[] = [];
|
|
const dest = new Writable({
|
|
write(chunk, _enc, cb) {
|
|
lines.push(chunk.toString());
|
|
cb();
|
|
},
|
|
});
|
|
return { logger: createLogger({ level: "info", destination: dest }), lines };
|
|
}
|
|
|
|
/** 内存 API Key 仓库 */
|
|
export function memoryStore(keys: Record<string, { id: string; scopes: string[]; revoked?: boolean }>): ApiKeyStore {
|
|
const byHash = new Map(Object.entries(keys).map(([k, v]) => [hashApiKey(k), v]));
|
|
return {
|
|
async findByHash(h) {
|
|
const v = byHash.get(h);
|
|
return v ? { id: v.id, name: v.id, scopes: v.scopes, revokedAt: v.revoked ? new Date() : null } : null;
|
|
},
|
|
async touch() {},
|
|
};
|
|
}
|
|
|
|
export function testApp(opts: { nodeEnv?: "development" | "production"; store?: ApiKeyStore } = {}) {
|
|
const bus = new EventBus(5);
|
|
const { logger, lines } = memoryLogger();
|
|
const app = createApp({
|
|
config: { NODE_ENV: opts.nodeEnv ?? "development", DEV_ADMIN_KEY: DEV_KEY },
|
|
logger,
|
|
bus,
|
|
role: "all",
|
|
version: "0.0.0-test",
|
|
...(opts.store ? { apiKeyStore: opts.store } : {}),
|
|
});
|
|
return { app, bus, lines };
|
|
}
|
|
|
|
/** 读取 JSON 响应体(测试里不做类型收窄) */
|
|
// biome-ignore lint/suspicious/noExplicitAny: 测试断言用
|
|
export const json = (res: Response): Promise<any> => res.json();
|