Skip to content

Commit 77166b5

Browse files
fullstackjamclaude
andauthored
test: adopt @cloudflare/vitest-pool-workers, delete SQL-parser mock (#9)
* test: introduce vitest-pool-workers for D1-backed integration tests Migrates 4 server tests from the SQL-parsing mock DB to real D1 running inside the Workers runtime via @cloudflare/vitest-pool-workers. Existing tests on happy-dom remain untouched. Changes: - Adds vitest.workers.config.ts (cloudflareTest plugin + cloudflarePool) scoped to the 4 migrated tests; filters out data-only migrations - Adds src/lib/test/{apply-migrations,seed,call}.ts as the new test infrastructure (real-D1 seeding, FK-aware reset, minimal RequestEvent builder) - Migrates health, db/configs, api/configs, and api/auth/cli/poll tests - Bumps vitest 4.0.18 -> 4.1.x (pool-workers peer dep) - Fixes migration 0002 to use ADD COLUMN + CREATE UNIQUE INDEX instead of ALTER TABLE ADD COLUMN ... UNIQUE (SQLite-illegal; hosted D1 had tolerated it). Safe for prod, already applied - Fixes 3 hidden test bugs where fixtures duplicated UNIQUE alias values; the SQL-parser mock didn't enforce the constraint After: 48 tests on pool-workers (4 files), 232 on happy-dom (16 files), all green. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * test: complete vitest-pool-workers migration, delete SQL-parser mock Migrates the remaining 9 server tests to vitest-pool-workers and removes the fragile SQL-parsing mock DB. Pure-library tests (rate-limit, install-script, validation) and archtests stay on happy-dom — they don't need D1 or the Workers runtime. Migrated to pool-workers in this commit: - src/lib/server/auth.test.ts (JWT + DB-backed getCurrentUser) - src/routes/api/auth/cli/start/server.test.ts - src/routes/api/auth/cli/approve/server.test.ts - src/routes/api/configs/[slug]/server.test.ts - src/routes/api/configs/[slug]/revisions/server.test.ts - src/routes/[username]/[slug]/install/server.test.ts - src/routes/[username]/[slug]/config/server.test.ts - src/smoke-tests/{cli-auth-flow,config-crud,critical-paths}.test.ts The CLI auth-flow smoke test now exercises the real approve endpoint end-to-end (previously had to fake it because the mock couldn't handle datetime expressions). The config-crud smoke test now verifies UPDATE side effects in the DB (previously couldn't — mock didn't honor COALESCE). Infrastructure: - Adds src/lib/test/call.ts helper to cut RequestEvent boilerplate - Extends call() with params/clientAddress for routes that need them - Deletes src/lib/test/db-mock.ts (~390 lines of SQL-parsing mock) - Deletes src/lib/test/setup.ts (process.env shims no longer needed) - Deletes src/lib/test/helpers.ts (unused after migration) - Trims src/lib/test/fixtures.ts to data-only exports - Adds @cloudflare/vitest-pool-workers/types to tsconfig.json - Wires npm test / test:run / test:coverage to run both configs; adds test:happy-dom and test:workers for targeted runs Final test layout: 181 tests on pool-workers (14 files, real D1 + Workers runtime), 99 tests on happy-dom (6 files, pure libs + archtests). Both green; full npm run validate clean. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * ci: bump Node to 22 and silence knip on cloudflare:test virtual module CI Node 20 → 22 because @cloudflare/vitest-pool-workers transitively pulls @cloudflare/kv-asset-handler@0.5.0 which requires Node ≥22. knip drift sensor was flagging every `cloudflare:test` import as an unlisted dependency (it doesn't grok the virtual-module convention). Added `cloudflare` to knip.json's ignoreDependencies so the sensor stops opening tracking issues for this. The package itself is provided by @cloudflare/vitest-pool-workers and not a real dependency entry. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix(test): skip v8 coverage on pool-workers suite workerd has no node:inspector/promises module, which @vitest/coverage-v8 requires. Coverage now runs only on the happy-dom suite; the pool-workers suite still runs (verifying behavior) but does not contribute to the coverage report. Trade-off: covered % drops because many tests moved to pool-workers. Acceptable — tests still execute, and switching the coverage backend (e.g. istanbul) is a separate decision. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent b6a3cf9 commit 77166b5

31 files changed

Lines changed: 2485 additions & 5417 deletions

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
- name: Setup Node.js
2121
uses: actions/setup-node@v4
2222
with:
23-
node-version: '20'
23+
node-version: '22'
2424

2525
- name: Install dependencies
2626
run: npm install --legacy-peer-deps

.github/workflows/deploy.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
- name: Setup Node.js
2222
uses: actions/setup-node@v4
2323
with:
24-
node-version: '20'
24+
node-version: '22'
2525

2626
- name: Install dependencies
2727
run: npm install --legacy-peer-deps

.github/workflows/harness.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ jobs:
3434
- uses: actions/checkout@v4
3535
- uses: actions/setup-node@v4
3636
with:
37-
node-version: '20'
37+
node-version: '22'
3838
- name: npm audit --audit-level=high
3939
# Fails on a high-or-critical advisory. Production-only because we
4040
# don't act on devDep advisories (no runtime exposure).
@@ -48,7 +48,7 @@ jobs:
4848
- uses: actions/checkout@v4
4949
- uses: actions/setup-node@v4
5050
with:
51-
node-version: '20'
51+
node-version: '22'
5252
- name: Install dependencies
5353
run: npm install --legacy-peer-deps --no-audit --no-fund
5454
- name: Run knip

knip.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"$schema": "https://unpkg.com/knip@5/schema.json",
33
"entry": ["src/lib/components/SearchModal.svelte!"],
4-
"ignoreDependencies": ["@cloudflare/workers-types"],
4+
"ignoreDependencies": ["@cloudflare/workers-types", "cloudflare"],
55
"rules": {
66
"exports": "off",
77
"types": "off",

migrations/0002_add_alias.sql

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@
22
-- Created: 2024-02-01
33
-- Note: SQLite doesn't support "IF NOT EXISTS" for ALTER TABLE ADD COLUMN
44
-- This migration may fail if already applied - that's expected behavior
5+
--
6+
-- 2026 update: SQLite forbids `ADD COLUMN ... UNIQUE` (hosted D1 silently
7+
-- tolerated this in the past, local Miniflare D1 rejects it strictly per
8+
-- the SQLite spec). Replaced with the canonical pattern:
9+
-- ADD COLUMN (no constraint) + CREATE UNIQUE INDEX. Equivalent uniqueness
10+
-- guarantee, runs on both hosted and local D1. Safe to re-apply on prod:
11+
-- this migration is already recorded in d1_migrations, so it will not
12+
-- re-execute against the hosted DB.
513

6-
-- Add alias column (will fail if exists - that's OK)
7-
ALTER TABLE configs ADD COLUMN alias TEXT UNIQUE;
14+
ALTER TABLE configs ADD COLUMN alias TEXT;
815

9-
-- Create index (idempotent)
10-
CREATE INDEX IF NOT EXISTS idx_configs_alias ON configs(alias);
16+
CREATE UNIQUE INDEX IF NOT EXISTS idx_configs_alias_unique ON configs(alias);

0 commit comments

Comments
 (0)