/* A migrated in-memory database with the shipped dictionary loaded — the same band files, through the same loader, as the app. */ import { vi } from "vitest"; import { readFileSync } from "node:fs"; import { SqliteWasmDb } from "@app/db/sqlite-wasm-core.js"; import { migrate } from "@app/db/migrate.js"; import type { Db } from "@app/db/types.js"; const DICT = new URL("../../app/public/dict/", import.meta.url); /** Serve app/public/dict/ to the loader's fetch. Undo with vi.unstubAllGlobals(). */ export function serveDictionary(): void { vi.stubGlobal("fetch", async (input: string) => { const name = String(input).split("/dict/")[1] ?? ""; return new Response(readFileSync(new URL(name, DICT))); }); } export async function dictionaryDb(upToBand = 1): Promise { serveDictionary(); const db = await SqliteWasmDb.open({ memory: true }); await migrate(db); const { ensureBands } = await import("@app/domain/dictionary.js"); await ensureBands(db, upToBand); return db; }