Files
Codewalkers/src/db/repositories/drizzle/test-helpers.ts
Lukas May 8da4e71075 feat(12-01): extend AgentMode with 'decompose'
- Add 'decompose' to AgentMode union type
- Update agents table mode column enum in database schema
- Update test-helpers.ts CREATE_TABLES_SQL with CHECK constraint
- Add missing getNextNumber implementation (blocking fix)
2026-02-01 11:32:18 +01:00

119 lines
3.3 KiB
TypeScript

/**
* Test helpers for repository tests.
*
* Provides utilities for setting up in-memory test databases
* with schema applied.
*/
import Database from 'better-sqlite3';
import { drizzle } from 'drizzle-orm/better-sqlite3';
import type { DrizzleDatabase } from '../../index.js';
import * as schema from '../../schema.js';
/**
* SQL statements to create the database schema.
* These mirror the schema defined in schema.ts.
*/
const CREATE_TABLES_SQL = `
-- Initiatives table
CREATE TABLE IF NOT EXISTS initiatives (
id TEXT PRIMARY KEY NOT NULL,
name TEXT NOT NULL,
description TEXT,
status TEXT NOT NULL DEFAULT 'active',
created_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL
);
-- Phases table
CREATE TABLE IF NOT EXISTS phases (
id TEXT PRIMARY KEY NOT NULL,
initiative_id TEXT NOT NULL REFERENCES initiatives(id) ON DELETE CASCADE,
number INTEGER NOT NULL,
name TEXT NOT NULL,
description TEXT,
status TEXT NOT NULL DEFAULT 'pending',
created_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL
);
-- Plans table
CREATE TABLE IF NOT EXISTS plans (
id TEXT PRIMARY KEY NOT NULL,
phase_id TEXT NOT NULL REFERENCES phases(id) ON DELETE CASCADE,
number INTEGER NOT NULL,
name TEXT NOT NULL,
description TEXT,
status TEXT NOT NULL DEFAULT 'pending',
created_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL
);
-- Tasks table
CREATE TABLE IF NOT EXISTS tasks (
id TEXT PRIMARY KEY NOT NULL,
plan_id TEXT NOT NULL REFERENCES plans(id) ON DELETE CASCADE,
name TEXT NOT NULL,
description TEXT,
type TEXT NOT NULL DEFAULT 'auto',
priority TEXT NOT NULL DEFAULT 'medium',
status TEXT NOT NULL DEFAULT 'pending',
"order" INTEGER NOT NULL DEFAULT 0,
created_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL
);
-- Task dependencies table
CREATE TABLE IF NOT EXISTS task_dependencies (
id TEXT PRIMARY KEY NOT NULL,
task_id TEXT NOT NULL REFERENCES tasks(id) ON DELETE CASCADE,
depends_on_task_id TEXT NOT NULL REFERENCES tasks(id) ON DELETE CASCADE,
created_at INTEGER NOT NULL
);
-- Agents table
CREATE TABLE IF NOT EXISTS agents (
id TEXT PRIMARY KEY NOT NULL,
name TEXT NOT NULL UNIQUE,
task_id TEXT REFERENCES tasks(id) ON DELETE SET NULL,
session_id TEXT,
worktree_id TEXT NOT NULL,
status TEXT NOT NULL DEFAULT 'idle',
mode TEXT NOT NULL DEFAULT 'execute' CHECK(mode IN ('execute', 'discuss', 'breakdown', 'decompose')),
created_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL
);
-- Messages table
CREATE TABLE IF NOT EXISTS messages (
id TEXT PRIMARY KEY NOT NULL,
sender_type TEXT NOT NULL,
sender_id TEXT REFERENCES agents(id) ON DELETE SET NULL,
recipient_type TEXT NOT NULL,
recipient_id TEXT REFERENCES agents(id) ON DELETE SET NULL,
type TEXT NOT NULL DEFAULT 'info',
content TEXT NOT NULL,
requires_response INTEGER NOT NULL DEFAULT 0,
status TEXT NOT NULL DEFAULT 'pending',
parent_message_id TEXT REFERENCES messages(id) ON DELETE SET NULL,
created_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL
);
`;
/**
* Create an in-memory test database with schema applied.
* Returns a fresh Drizzle instance for each call.
*/
export function createTestDatabase(): DrizzleDatabase {
const sqlite = new Database(':memory:');
// Enable foreign keys
sqlite.pragma('foreign_keys = ON');
// Create all tables
sqlite.exec(CREATE_TABLES_SQL);
return drizzle(sqlite, { schema });
}