feat(14-06): convert route tests + MCP tests to async PGlite

- All 8 route test files: async createTestApp(), async beforeEach
- MCP tools test: await createTestDb(), await getCollectionSummary()
- Fixed MCP tool files: added await to all service calls in items, categories, threads, setups tools
- Fixed MCP collection resource: made getCollectionSummary async
- Fixed MCP index.ts: await getCollectionSummary call
- Increased test timeout to 30s in bunfig.toml for PGlite WASM overhead
- Zero SQLite references remain in tests/

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-04 15:40:14 +02:00
parent 458b33f1c7
commit f30d375544
15 changed files with 80 additions and 79 deletions

View File

@@ -1,2 +1,3 @@
[test]
root = "tests/"
timeout = 30_000

View File

@@ -65,7 +65,7 @@ function createMcpServer(db: Db): McpServer {
mimeType: "application/json",
},
async () => {
const summary = getCollectionSummary(db);
const summary = await getCollectionSummary(db);
return {
contents: [
{

View File

@@ -7,12 +7,12 @@ import { getGlobalTotals } from "../../services/totals.service.ts";
type Db = typeof prodDb;
export function getCollectionSummary(db: Db) {
const totals = getGlobalTotals(db);
const categories = getAllCategories(db);
const items = getAllItems(db);
const setups = getAllSetups(db);
const activeThreads = getAllThreads(db, false);
export async function getCollectionSummary(db: Db) {
const totals = await getGlobalTotals(db);
const categories = await getAllCategories(db);
const items = await getAllItems(db);
const setups = await getAllSetups(db);
const activeThreads = await getAllThreads(db, false);
// Build items-by-category map
const itemsByCategory: Record<string, number> = {};

View File

@@ -41,7 +41,7 @@ export function registerCategoryTools(db: Db) {
return {
list_categories: async (): Promise<ToolResult> => {
try {
const cats = getAllCategories(db);
const cats = await getAllCategories(db);
return textResult(cats);
} catch (err) {
return errorResult((err as Error).message);
@@ -53,7 +53,7 @@ export function registerCategoryTools(db: Db) {
icon?: string;
}): Promise<ToolResult> => {
try {
const cat = createCategory(db, args);
const cat = await createCategory(db, args);
return textResult(cat);
} catch (err) {
return errorResult((err as Error).message);

View File

@@ -95,7 +95,7 @@ export function registerItemTools(db: Db) {
return {
list_items: async (args: { categoryId?: number }): Promise<ToolResult> => {
try {
const items = getAllItems(db);
const items = await getAllItems(db);
if (args.categoryId) {
return textResult(
items.filter((i) => i.categoryId === args.categoryId),
@@ -109,7 +109,7 @@ export function registerItemTools(db: Db) {
get_item: async (args: { id: number }): Promise<ToolResult> => {
try {
const item = getItemById(db, args.id);
const item = await getItemById(db, args.id);
if (!item) return errorResult(`Item ${args.id} not found`);
return textResult(item);
} catch (err) {
@@ -128,7 +128,7 @@ export function registerItemTools(db: Db) {
imageSourceUrl?: string;
}): Promise<ToolResult> => {
try {
const item = createItem(db, args);
const item = await createItem(db, args);
return textResult(item);
} catch (err) {
return errorResult((err as Error).message);
@@ -148,7 +148,7 @@ export function registerItemTools(db: Db) {
}): Promise<ToolResult> => {
try {
const { id, ...data } = args;
const item = updateItem(db, id, data);
const item = await updateItem(db, id, data);
if (!item) return errorResult(`Item ${id} not found`);
return textResult(item);
} catch (err) {
@@ -158,7 +158,7 @@ export function registerItemTools(db: Db) {
delete_item: async (args: { id: number }): Promise<ToolResult> => {
try {
const item = deleteItem(db, args.id);
const item = await deleteItem(db, args.id);
if (!item) return errorResult(`Item ${args.id} not found`);
return textResult({ deleted: true, item });
} catch (err) {

View File

@@ -64,7 +64,7 @@ export function registerSetupTools(db: Db) {
return {
list_setups: async (): Promise<ToolResult> => {
try {
const setupList = getAllSetups(db);
const setupList = await getAllSetups(db);
return textResult(setupList);
} catch (err) {
return errorResult((err as Error).message);
@@ -73,7 +73,7 @@ export function registerSetupTools(db: Db) {
get_setup: async (args: { id: number }): Promise<ToolResult> => {
try {
const setup = getSetupWithItems(db, args.id);
const setup = await getSetupWithItems(db, args.id);
if (!setup) return errorResult(`Setup ${args.id} not found`);
return textResult(setup);
} catch (err) {
@@ -83,7 +83,7 @@ export function registerSetupTools(db: Db) {
create_setup: async (args: { name: string }): Promise<ToolResult> => {
try {
const setup = createSetup(db, args);
const setup = await createSetup(db, args);
return textResult(setup);
} catch (err) {
return errorResult((err as Error).message);
@@ -98,14 +98,14 @@ export function registerSetupTools(db: Db) {
try {
let setup = null;
if (args.name) {
setup = updateSetup(db, args.id, { name: args.name });
setup = await updateSetup(db, args.id, { name: args.name });
if (!setup) return errorResult(`Setup ${args.id} not found`);
}
if (args.itemIds) {
syncSetupItems(db, args.id, args.itemIds);
await syncSetupItems(db, args.id, args.itemIds);
}
// Return updated setup with items
const result = getSetupWithItems(db, args.id);
const result = await getSetupWithItems(db, args.id);
if (!result) return errorResult(`Setup ${args.id} not found`);
return textResult(result);
} catch (err) {

View File

@@ -119,7 +119,7 @@ export function registerThreadTools(db: Db) {
includeResolved?: boolean;
}): Promise<ToolResult> => {
try {
const threadList = getAllThreads(db, args.includeResolved ?? false);
const threadList = await getAllThreads(db, args.includeResolved ?? false);
return textResult(threadList);
} catch (err) {
return errorResult((err as Error).message);
@@ -128,7 +128,7 @@ export function registerThreadTools(db: Db) {
get_thread: async (args: { id: number }): Promise<ToolResult> => {
try {
const thread = getThreadWithCandidates(db, args.id);
const thread = await getThreadWithCandidates(db, args.id);
if (!thread) return errorResult(`Thread ${args.id} not found`);
return textResult(thread);
} catch (err) {
@@ -141,7 +141,7 @@ export function registerThreadTools(db: Db) {
categoryId: number;
}): Promise<ToolResult> => {
try {
const thread = createThread(db, args);
const thread = await createThread(db, args);
return textResult(thread);
} catch (err) {
return errorResult((err as Error).message);
@@ -153,7 +153,7 @@ export function registerThreadTools(db: Db) {
candidateId: number;
}): Promise<ToolResult> => {
try {
const result = resolveThread(db, args.threadId, args.candidateId);
const result = await resolveThread(db, args.threadId, args.candidateId);
if (!result.success) {
return errorResult(result.error ?? "Failed to resolve thread");
}
@@ -177,7 +177,7 @@ export function registerThreadTools(db: Db) {
}): Promise<ToolResult> => {
try {
const { threadId, ...data } = args;
const candidate = createCandidate(db, threadId, data);
const candidate = await createCandidate(db, threadId, data);
return textResult(candidate);
} catch (err) {
return errorResult((err as Error).message);
@@ -200,7 +200,7 @@ export function registerThreadTools(db: Db) {
}): Promise<ToolResult> => {
try {
const { id, ...data } = args;
const candidate = updateCandidate(db, id, data);
const candidate = await updateCandidate(db, id, data);
if (!candidate) return errorResult(`Candidate ${id} not found`);
return textResult(candidate);
} catch (err) {
@@ -210,7 +210,7 @@ export function registerThreadTools(db: Db) {
remove_candidate: async (args: { id: number }): Promise<ToolResult> => {
try {
const candidate = deleteCandidate(db, args.id);
const candidate = await deleteCandidate(db, args.id);
if (!candidate) return errorResult(`Candidate ${args.id} not found`);
return textResult({ deleted: true, candidate });
} catch (err) {

View File

@@ -14,7 +14,7 @@ function parseResult(result: {
describe("MCP Item Tools", () => {
test("list_items returns array", async () => {
const db = createTestDb();
const db = await createTestDb();
const tools = registerItemTools(db);
const result = await tools.list_items({});
const data = parseResult(result);
@@ -22,7 +22,7 @@ describe("MCP Item Tools", () => {
});
test("create_item creates and returns item", async () => {
const db = createTestDb();
const db = await createTestDb();
const tools = registerItemTools(db);
const result = await tools.create_item({
name: "Test Tent",
@@ -38,7 +38,7 @@ describe("MCP Item Tools", () => {
});
test("get_item retrieves by ID", async () => {
const db = createTestDb();
const db = await createTestDb();
const tools = registerItemTools(db);
const created = parseResult(
await tools.create_item({ name: "Sleeping Bag", categoryId: 1 }),
@@ -50,7 +50,7 @@ describe("MCP Item Tools", () => {
});
test("get_item returns error for missing item", async () => {
const db = createTestDb();
const db = await createTestDb();
const tools = registerItemTools(db);
const result = await tools.get_item({ id: 999 });
const data = parseResult(result);
@@ -58,7 +58,7 @@ describe("MCP Item Tools", () => {
});
test("delete_item removes item", async () => {
const db = createTestDb();
const db = await createTestDb();
const tools = registerItemTools(db);
const created = parseResult(
await tools.create_item({ name: "To Delete", categoryId: 1 }),
@@ -76,7 +76,7 @@ describe("MCP Item Tools", () => {
describe("MCP Category Tools", () => {
test("list_categories returns array with Uncategorized", async () => {
const db = createTestDb();
const db = await createTestDb();
const tools = registerCategoryTools(db);
const result = await tools.list_categories();
const data = parseResult(result);
@@ -86,7 +86,7 @@ describe("MCP Category Tools", () => {
});
test("create_category creates a new category", async () => {
const db = createTestDb();
const db = await createTestDb();
const tools = registerCategoryTools(db);
const result = await tools.create_category({
name: "Shelter",
@@ -100,7 +100,7 @@ describe("MCP Category Tools", () => {
describe("MCP Thread Tools", () => {
test("create_thread starts a thread with status active", async () => {
const db = createTestDb();
const db = await createTestDb();
const tools = registerThreadTools(db);
const result = await tools.create_thread({
name: "Handlebar Bag",
@@ -112,7 +112,7 @@ describe("MCP Thread Tools", () => {
});
test("add_candidate adds to thread", async () => {
const db = createTestDb();
const db = await createTestDb();
const tools = registerThreadTools(db);
const thread = parseResult(
await tools.create_thread({ name: "Saddle Bag", categoryId: 1 }),
@@ -132,7 +132,7 @@ describe("MCP Thread Tools", () => {
});
test("resolve_thread picks winner and creates item", async () => {
const db = createTestDb();
const db = await createTestDb();
const threadTools = registerThreadTools(db);
const itemTools = registerItemTools(db);
@@ -179,7 +179,7 @@ describe("MCP Thread Tools", () => {
describe("MCP Setup Tools", () => {
test("create_setup and list_setups", async () => {
const db = createTestDb();
const db = await createTestDb();
const tools = registerSetupTools(db);
await tools.create_setup({ name: "Weekend Trip" });
const result = await tools.list_setups();
@@ -189,7 +189,7 @@ describe("MCP Setup Tools", () => {
});
test("get_setup returns setup with items", async () => {
const db = createTestDb();
const db = await createTestDb();
const setupTools = registerSetupTools(db);
const itemTools = registerItemTools(db);
@@ -210,10 +210,10 @@ describe("MCP Setup Tools", () => {
});
describe("MCP Collection Summary Resource", () => {
test("returns overview with correct counts", () => {
const db = createTestDb();
test("returns overview with correct counts", async () => {
const db = await createTestDb();
const summary = getCollectionSummary(db);
const summary = await getCollectionSummary(db);
expect(summary.overview).toBeDefined();
expect(summary.overview.totalItems).toBe(0);
expect(summary.overview.categoryCount).toBe(1); // Uncategorized
@@ -223,7 +223,7 @@ describe("MCP Collection Summary Resource", () => {
});
test("reflects items and threads after creation", async () => {
const db = createTestDb();
const db = await createTestDb();
const itemTools = registerItemTools(db);
const threadTools = registerThreadTools(db);
@@ -242,7 +242,7 @@ describe("MCP Collection Summary Resource", () => {
categoryId: 1,
});
const summary = getCollectionSummary(db);
const summary = await getCollectionSummary(db);
expect(summary.overview.totalItems).toBe(2);
expect(summary.overview.totalWeightGrams).toBe(2000);
expect(summary.overview.activeThreadCount).toBe(1);

View File

@@ -3,8 +3,8 @@ import { Hono } from "hono";
import { authRoutes } from "../../src/server/routes/auth.ts";
import { createTestDb } from "../helpers/db.ts";
function createTestApp() {
const db = createTestDb();
async function createTestApp() {
const db = await createTestDb();
const app = new Hono<{ Variables: { db?: any } }>();
app.use("*", async (c, next) => {
@@ -19,8 +19,8 @@ function createTestApp() {
describe("Auth Routes", () => {
let app: Hono;
beforeEach(() => {
const testApp = createTestApp();
beforeEach(async () => {
const testApp = await createTestApp();
app = testApp.app;
});

View File

@@ -4,8 +4,8 @@ import { categoryRoutes } from "../../src/server/routes/categories.ts";
import { itemRoutes } from "../../src/server/routes/items.ts";
import { createTestDb } from "../helpers/db.ts";
function createTestApp() {
const db = createTestDb();
async function createTestApp() {
const db = await createTestDb();
const app = new Hono();
// Inject test DB into context for all routes
@@ -22,8 +22,8 @@ function createTestApp() {
describe("Category Routes", () => {
let app: Hono;
beforeEach(() => {
const testApp = createTestApp();
beforeEach(async () => {
const testApp = await createTestApp();
app = testApp.app;
});

View File

@@ -4,8 +4,8 @@ import { categoryRoutes } from "../../src/server/routes/categories.ts";
import { itemRoutes } from "../../src/server/routes/items.ts";
import { createTestDb } from "../helpers/db.ts";
function createTestApp() {
const db = createTestDb();
async function createTestApp() {
const db = await createTestDb();
const app = new Hono();
// Inject test DB into context for all routes
@@ -22,8 +22,8 @@ function createTestApp() {
describe("Item Routes", () => {
let app: Hono;
beforeEach(() => {
const testApp = createTestApp();
beforeEach(async () => {
const testApp = await createTestApp();
app = testApp.app;
});

View File

@@ -6,8 +6,8 @@ import { oauthRoutes, wellKnownRoute } from "../../src/server/routes/oauth.ts";
import { createUser } from "../../src/server/services/auth.service.ts";
import { createTestDb } from "../helpers/db.ts";
function createTestApp() {
const db = createTestDb();
async function createTestApp() {
const db = await createTestDb();
const app = new Hono<{ Variables: { db?: any } }>();
app.use("*", async (c, next) => {
c.set("db", db);
@@ -18,8 +18,8 @@ function createTestApp() {
return { app, db };
}
function createFullTestApp() {
const db = createTestDb();
async function createFullTestApp() {
const db = await createTestDb();
const app = new Hono<{ Variables: { db?: any } }>();
app.use("*", async (c, next) => {
c.set("db", db);
@@ -39,10 +39,10 @@ function generatePkce() {
describe("OAuth Routes", () => {
let app: Hono;
let db: ReturnType<typeof createTestDb>;
let db: any;
beforeEach(async () => {
const testApp = createTestApp();
const testApp = await createTestApp();
app = testApp.app;
db = testApp.db;
await createUser(db, "admin", "secret123");
@@ -182,7 +182,7 @@ describe("OAuth Routes", () => {
});
describe("Full OAuth flow", () => {
it("register authorize token exchange", async () => {
it("register -> authorize -> token exchange", async () => {
// 1. Register client
const regRes = await app.request("/oauth/register", {
method: "POST",
@@ -245,9 +245,9 @@ describe("OAuth Routes", () => {
});
});
describe("Full OAuth MCP Flow", () => {
it("complete flow: register authorize token MCP call", async () => {
const { app, db } = createFullTestApp();
describe("Full OAuth -> MCP Flow", () => {
it("complete flow: register -> authorize -> token -> MCP call", async () => {
const { app, db } = await createFullTestApp();
await createUser(db, "admin", "secret123");
const { verifier, challenge } = generatePkce();
@@ -321,7 +321,7 @@ describe("OAuth Routes", () => {
});
it("rejects MCP call without auth when user exists", async () => {
const { app, db } = createFullTestApp();
const { app, db } = await createFullTestApp();
await createUser(db, "admin", "secret123");
const mcpRes = await app.request("/mcp", {

View File

@@ -6,8 +6,8 @@ import { setupRoutes } from "../../src/server/routes/setups";
import { threadRoutes } from "../../src/server/routes/threads";
import { createTestDb } from "../helpers/db";
function createTestApp() {
const db = createTestDb();
async function createTestApp() {
const db = await createTestDb();
const app = new Hono();
app.use("*", async (c, next) => {
c.set("db", db);
@@ -23,8 +23,8 @@ function createTestApp() {
describe("Invalid ID parameter handling", () => {
let app: Hono;
beforeEach(() => {
app = createTestApp();
beforeEach(async () => {
app = await createTestApp();
});
describe("items", () => {

View File

@@ -4,8 +4,8 @@ import { itemRoutes } from "../../src/server/routes/items.ts";
import { setupRoutes } from "../../src/server/routes/setups.ts";
import { createTestDb } from "../helpers/db.ts";
function createTestApp() {
const db = createTestDb();
async function createTestApp() {
const db = await createTestDb();
const app = new Hono();
app.use("*", async (c, next) => {
@@ -39,8 +39,8 @@ async function createItemViaAPI(app: Hono, data: any) {
describe("Setup Routes", () => {
let app: Hono;
beforeEach(() => {
const testApp = createTestApp();
beforeEach(async () => {
const testApp = await createTestApp();
app = testApp.app;
});

View File

@@ -3,8 +3,8 @@ import { Hono } from "hono";
import { threadRoutes } from "../../src/server/routes/threads.ts";
import { createTestDb } from "../helpers/db.ts";
function createTestApp() {
const db = createTestDb();
async function createTestApp() {
const db = await createTestDb();
const app = new Hono();
// Inject test DB into context for all routes
@@ -38,8 +38,8 @@ async function createCandidateViaAPI(app: Hono, threadId: number, data: any) {
describe("Thread Routes", () => {
let app: Hono;
beforeEach(() => {
const testApp = createTestApp();
beforeEach(async () => {
const testApp = await createTestApp();
app = testApp.app;
});