From dd3df1b5e3bc9426c0208eda4774c5c95f67419e Mon Sep 17 00:00:00 2001 From: Jack Herrington Date: Wed, 2 Dec 2026 17:26:43 -0800 Subject: [PATCH 1/3] perf(test): share one PGlite VM across tests via prefix isolation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Measured: a PGlite VM costs ~400ms to boot (WASM + baked catalog) while the CMS migrations on top are only ~13ms — so the test cost was dominated by booting a fresh VM per `defineCms`, not by schema work. The `@tanstack/cms-store-postgres/testing` setup now shares ONE in-memory VM per process and gives each config an isolated schema via a table prefix keyed by its resolved data dir. This mirrors the old embedded default's semantics exactly (same dataDir shares — so close+reopen still sees its data — distinct dataDirs isolate), but pays the 400ms boot once instead of per config. A passed-in pool isn't closed by storage.close(), so the shared VM survives per-config teardown. Result: core 48s→28s, api ~26s→5.5s; no test changes needed. app/base keeps on-disk persistence via the new `./testing-disk` variant — its recover-owner test spawns a subprocess that reads the database file, which an in-memory VM can't serve. Co-Authored-By: Claude Opus 4.8 (1M context) --- app/base/vitest.config.ts | 8 ++-- packages/store-postgres/package.json | 5 +++ packages/store-postgres/src/testing-disk.ts | 14 +++++++ packages/store-postgres/src/testing.ts | 44 +++++++++++++++++++-- packages/store-postgres/tsdown.config.ts | 7 +++- 5 files changed, 70 insertions(+), 8 deletions(-) create mode 100644 packages/store-postgres/src/testing-disk.ts diff --git a/app/base/vitest.config.ts b/app/base/vitest.config.ts index dea67788..da811eab 100644 --- a/app/base/vitest.config.ts +++ b/app/base/vitest.config.ts @@ -3,9 +3,11 @@ export default defineConfig({ test: { name: 'base', environment: 'node', - // Core ships no storage default; restore the embedded PGlite engine - // for tests that don't inject their own `storage`. - setupFiles: ['@tanstack/cms-store-postgres/testing'], + // Core ships no storage default; restore the embedded PGlite engine for + // tests that don't inject their own `storage`. Base uses the on-disk + // variant: the recover-owner test spawns a subprocess that reads the + // database file, so it needs real persistence, not the shared in-memory VM. + setupFiles: ['@tanstack/cms-store-postgres/testing-disk'], include: ['tests/**/*.test.ts'], fileParallelism: false, // Tests drive the embedded PGlite engine (WASM init) and multi-step diff --git a/packages/store-postgres/package.json b/packages/store-postgres/package.json index 8bee6471..67d55c78 100644 --- a/packages/store-postgres/package.json +++ b/packages/store-postgres/package.json @@ -23,6 +23,7 @@ ".": "./src/index.ts", "./pglite": "./src/pglite.ts", "./testing": "./src/testing.ts", + "./testing-disk": "./src/testing-disk.ts", "./package.json": "./package.json" }, "main": "./src/index.ts", @@ -47,6 +48,10 @@ "types": "./dist/testing.d.ts", "import": "./dist/testing.js" }, + "./testing-disk": { + "types": "./dist/testing-disk.d.ts", + "import": "./dist/testing-disk.js" + }, "./package.json": "./package.json" }, "main": "./dist/index.js", diff --git a/packages/store-postgres/src/testing-disk.ts b/packages/store-postgres/src/testing-disk.ts new file mode 100644 index 00000000..e7587180 --- /dev/null +++ b/packages/store-postgres/src/testing-disk.ts @@ -0,0 +1,14 @@ +/** + * Vitest setup variant that wires the default storage to an **on-disk** PGlite + * per `/pglite` (one VM per data dir). Use this instead of + * `@tanstack/cms-store-postgres/testing` (shared in-memory) when a suite needs + * real file persistence — e.g. an integration test that spawns a subprocess + * reading the same database. Slower (each distinct `dataDir` boots a VM), but + * faithful to a deployed app's on-disk engine. + * + * test: { setupFiles: ['@tanstack/cms-store-postgres/testing-disk'] } + */ +import { setDefaultStorageFactory } from '@tanstack/cms-core/server' +import { createPgliteStorage } from './pglite' + +setDefaultStorageFactory(createPgliteStorage) diff --git a/packages/store-postgres/src/testing.ts b/packages/store-postgres/src/testing.ts index 3a8d687d..d235ef3d 100644 --- a/packages/store-postgres/src/testing.ts +++ b/packages/store-postgres/src/testing.ts @@ -1,15 +1,51 @@ /** * Vitest setup module: restore the embedded Postgres engine (PGlite) as the * process-wide default storage, so tests that call `defineCms(...)` without an - * explicit `storage` get the embedded engine — exactly the pre-seam dev - * default. Reference it from a package's vitest config: + * explicit `storage` get the embedded engine. Reference it from a package's + * vitest config: * * test: { setupFiles: ['@tanstack/cms-store-postgres/testing'] } * + * Booting a PGlite VM costs ~400ms (WASM + baked catalog); the CMS migrations + * on top are only ~13ms. So rather than a fresh VM per `defineCms`, this shares + * ONE in-memory VM per test process and gives each config an isolated, prefixed + * schema — the 400ms boot is paid once, each config pays ~13ms. A passed-in + * pool is never closed by `storage.close()` (see createPostgresStorage), so the + * shared VM survives per-config teardown and is dropped when the process exits. + * + * Isolation is keyed by the resolved data directory, exactly mirroring the old + * embedded default (which keyed its on-disk PGlite by `/pglite`): + * configs with the same `dataDir` share a schema — so a close + re-`defineCms` + * at the same `dataDir` still sees its data (cross-restart tests) — and distinct + * `dataDir`s are isolated. Tests wanting on-disk files inject an explicit + * `createPgliteStorage({ dataDir })` and get their own VM. + * * This is test-only wiring. Shipped apps inject `storage` explicitly; core * never imports a storage package of its own. */ import { setDefaultStorageFactory } from '@tanstack/cms-core/server' -import { createPgliteStorage } from './pglite' +import { createPostgresStorage } from './index' +import { createPglitePool } from './pglite' +import type { PgPoolLike } from './types' + +let sharedPool: PgPoolLike | undefined +let seq = 0 +// Resolved data dir -> table prefix, so the same dir always maps to the same +// schema (persistence across reopen) and distinct dirs never collide. +const prefixByDataDir = new Map() -setDefaultStorageFactory(createPgliteStorage) +setDefaultStorageFactory((ctx) => { + // One in-memory PGlite VM for the whole process; opened lazily on first query. + if (!sharedPool) sharedPool = createPglitePool() + let prefix = prefixByDataDir.get(ctx.dataDir) + if (!prefix) { + prefix = `t${(seq++).toString(36)}_cms_` + prefixByDataDir.set(ctx.dataDir, prefix) + } + return createPostgresStorage({ + pool: sharedPool, + migrationPool: sharedPool, + acquisitionTimeoutMs: 30_000, + prefix, + }) +}) diff --git a/packages/store-postgres/tsdown.config.ts b/packages/store-postgres/tsdown.config.ts index a46eec58..ad29bd1b 100644 --- a/packages/store-postgres/tsdown.config.ts +++ b/packages/store-postgres/tsdown.config.ts @@ -1,7 +1,12 @@ import { defineConfig } from 'tsdown' export default defineConfig({ - entry: ['src/index.ts', 'src/pglite.ts', 'src/testing.ts'], + entry: [ + 'src/index.ts', + 'src/pglite.ts', + 'src/testing.ts', + 'src/testing-disk.ts', + ], format: 'esm', platform: 'neutral', unbundle: true, From 0ef0faef4afcafd0aecb6f3ddbb4026cdcdb3916 Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Thu, 3 Dec 2026 01:28:00 +0000 Subject: [PATCH 2/3] ci: apply automated fixes --- kody/blog/src/entry.ts | 12 ++++++-- kody/blog/src/render.ts | 15 +++++++-- packages/core/src/read.ts | 12 ++++++-- packages/core/tests/read.test.ts | 52 +++++++++++++++++++++++--------- 4 files changed, 68 insertions(+), 23 deletions(-) diff --git a/kody/blog/src/entry.ts b/kody/blog/src/entry.ts index 4ada921e..d917dd35 100644 --- a/kody/blog/src/entry.ts +++ b/kody/blog/src/entry.ts @@ -34,7 +34,9 @@ export default { if (!(await isOwner())) return new Response('owner only', { status: 403 }) const body = (await request.text()).trim() if (!/^postgres(ql)?:\/\//.test(body)) { - return new Response('expected a postgres:// connection string', { status: 400 }) + return new Response('expected a postgres:// connection string', { + status: 400, + }) } await setConnectionString(body) return Response.json({ ok: true, saved: true }) @@ -54,8 +56,12 @@ export default { const postMatch = /\/post\/([^/]+)$/.exec(path) if (postMatch) { const id = decodeURIComponent(postMatch[1]) - const doc = await getDocument(store, 'post', id, { publishedOnly: true }) - return doc ? page(renderPost(base, doc)) : page(renderNotFound(base), 404) + const doc = await getDocument(store, 'post', id, { + publishedOnly: true, + }) + return doc + ? page(renderPost(base, doc)) + : page(renderNotFound(base), 404) } if (path === '/' || path === '') { diff --git a/kody/blog/src/render.ts b/kody/blog/src/render.ts index 50f683ff..2d70ae22 100644 --- a/kody/blog/src/render.ts +++ b/kody/blog/src/render.ts @@ -27,7 +27,9 @@ export function renderHome( .map( ({ id, doc }) => `
  • ${escapeHtml(doc.title)}${ - str(doc.excerpt) ? ` — ${escapeHtml(doc.excerpt)}` : '' + str(doc.excerpt) + ? ` — ${escapeHtml(doc.excerpt)}` + : '' }
  • `, ) .join('')}` @@ -35,7 +37,10 @@ export function renderHome( } export function renderPost(base: string, doc: Doc): string { - const meta = [str(doc.author) ? `by ${escapeHtml(doc.author)}` : '', escapeHtml(doc.publishedDate)] + const meta = [ + str(doc.author) ? `by ${escapeHtml(doc.author)}` : '', + escapeHtml(doc.publishedDate), + ] .filter(Boolean) .join(' · ') const body = `

    ${escapeHtml(doc.title)}

    ${ @@ -45,7 +50,11 @@ export function renderPost(base: string, doc: Doc): string { } export function renderNotFound(base: string): string { - return layout(base, 'Not found', `

    Not found

    ← home

    `) + return layout( + base, + 'Not found', + `

    Not found

    ← home

    `, + ) } const CSS = ` diff --git a/packages/core/src/read.ts b/packages/core/src/read.ts index 6ecca766..1f9033be 100644 --- a/packages/core/src/read.ts +++ b/packages/core/src/read.ts @@ -73,17 +73,23 @@ export type BlockRenderer = (block: Block, ctx: RenderContext) => string export const defaultBlockRenderers: Record = { hero: (b, c) => `

    ${c.escapeHtml(b.heading)}

    ${ - asString(b.subheading) ? `

    ${c.escapeHtml(b.subheading)}

    ` : '' + asString(b.subheading) + ? `

    ${c.escapeHtml(b.subheading)}

    ` + : '' }
    `, richText: (b, c) => c.renderMarkdown(asString(b.markdown)), quote: (b, c) => `

    ${c.escapeHtml(b.text)}

    ${ - asString(b.attribution) ? `${c.escapeHtml(b.attribution)}` : '' + asString(b.attribution) + ? `${c.escapeHtml(b.attribution)}` + : '' }
    `, image: (b, c) => asString(b.image) ? `
    ${c.escapeHtml(b.caption)}${ - asString(b.caption) ? `
    ${c.escapeHtml(b.caption)}
    ` : '' + asString(b.caption) + ? `
    ${c.escapeHtml(b.caption)}
    ` + : '' }
    ` : '', cta: (b, c) => diff --git a/packages/core/tests/read.test.ts b/packages/core/tests/read.test.ts index 0b9f8aad..fcc78d46 100644 --- a/packages/core/tests/read.test.ts +++ b/packages/core/tests/read.test.ts @@ -13,14 +13,18 @@ import { describe('read: html + markdown primitives', () => { it('escapes html-significant characters', () => { - expect(escapeHtml('&y')).toBe('<a href="x">&y') + expect(escapeHtml('&y')).toBe( + '<a href="x">&y', + ) expect(escapeHtml(null)).toBe('') expect(escapeHtml(42)).toBe('') }) it('renders tiny markdown (paragraphs, bold, code)', () => { expect(renderMarkdown('a\n\nb')).toBe('

    a

    b

    ') - expect(renderMarkdown('**bold** and `x`')).toBe('

    bold and x

    ') + expect(renderMarkdown('**bold** and `x`')).toBe( + '

    bold and x

    ', + ) expect(renderMarkdown('')).toBe('') // escapes before formatting expect(renderMarkdown('