Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

test: derive reset helper from Postgres catalog, not schema registry

justin481bf919b66a
diff --git a/apps/api/src/database/index.ts b/apps/api/src/database/index.ts
index c6d8edeb8..69731b0d0 100644
--- a/apps/api/src/database/index.ts
+++ b/apps/api/src/database/index.ts
@@ -47,10 +47,12 @@ import {
integrationTable,
invitationTable,
labelTable,
+ mcpOauthStateTable,
notificationTable,
projectTable,
sessionTable,
taskRelationTable,
+ taskReminderSentTable,
taskTable,
teamMemberTable,
teamTable,
@@ -87,10 +89,12 @@ export const schema = {
integrationTable,
invitationTable,
labelTable,
+ mcpOauthStateTable,
notificationTable,
projectTable,
sessionTable,
taskRelationTable,
+ taskReminderSentTable,
taskTable,
teamMemberTable,
teamTable,
diff --git a/tests/api-integration/helpers/database.test.ts b/tests/api-integration/helpers/database.test.ts
new file mode 100644
index 000000000..b803af027
--- /dev/null
+++ b/tests/api-integration/helpers/database.test.ts
@@ -0,0 +1,120 @@
+import { randomUUID } from "node:crypto";
+import { sql } from "drizzle-orm";
+import { afterEach, beforeEach, describe, expect, it } from "vitest";
+import db, { schema } from "../../../apps/api/src/database";
+import {
+ mcpOauthStateTable,
+ taskReminderSentTable,
+} from "../../../apps/api/src/database/schema";
+import { resetTestDatabase } from "./database";
+
+async function seedMcpOauthStateRow(): Promise<string> {
+ const id = `mcp-oauth-${randomUUID()}`;
+ await db.insert(mcpOauthStateTable).values({
+ id,
+ kind: "test",
+ key: `key-${randomUUID()}`,
+ payload: {},
+ expiresAt: new Date(Date.now() + 60_000),
+ });
+ return id;
+}
+
+async function seedTaskReminderSentRow(): Promise<string> {
+ const userId = `user-${randomUUID()}`;
+ const workspaceId = `workspace-${randomUUID()}`;
+ const projectId = `project-${randomUUID()}`;
+ const columnId = `column-${randomUUID()}`;
+ const taskId = `task-${randomUUID()}`;
+ const id = `task-reminder-${randomUUID()}`;
+
+ await db.insert(schema.userTable).values({
+ id: userId,
+ email: `${userId}@example.com`,
+ emailVerified: true,
+ name: "Reminder Test User",
+ });
+ await db.insert(schema.workspaceTable).values({
+ id: workspaceId,
+ createdAt: new Date(),
+ name: "Reminder Workspace",
+ slug: `workspace-${randomUUID()}`,
+ });
+ await db.insert(schema.workspaceUserTable).values({
+ workspaceId,
+ userId,
+ role: "admin",
+ joinedAt: new Date(),
+ });
+ await db.insert(schema.projectTable).values({
+ id: projectId,
+ workspaceId,
+ name: "Reminder Project",
+ icon: "Folder",
+ slug: `project-${randomUUID()}`,
+ });
+ await db.insert(schema.columnTable).values({
+ id: columnId,
+ projectId,
+ name: "To do",
+ slug: "to-do",
+ position: 1,
+ isFinal: false,
+ });
+ await db.insert(schema.taskTable).values({
+ id: taskId,
+ projectId,
+ title: "Reminder task",
+ description: "",
+ priority: "low",
+ status: "to-do",
+ columnId,
+ number: 1,
+ position: 1,
+ });
+ await db.insert(taskReminderSentTable).values({
+ id,
+ taskId,
+ reminderType: "due-soon",
+ });
+
+ return id;
+}
+
+async function rowExists(tableName: string, id: string): Promise<boolean> {
+ const result = await db.execute<{ exists: boolean }>(sql`
+ SELECT EXISTS (
+ SELECT 1 FROM ${sql.raw(`"${tableName}"`)}
+ WHERE id = ${id}
+ ) AS exists
+ `);
+ return result.rows[0]?.exists === true;
+}
+
+describe("resetTestDatabase", () => {
+ beforeEach(async () => {
+ await resetTestDatabase();
+ });
+
+ afterEach(async () => {
+ await resetTestDatabase();
+ });
+
+ it("truncates mcp_oauth_state rows", async () => {
+ const id = await seedMcpOauthStateRow();
+ expect(await rowExists("mcp_oauth_state", id)).toBe(true);
+
+ await resetTestDatabase();
+
+ expect(await rowExists("mcp_oauth_state", id)).toBe(false);
+ });
+
+ it("truncates task_reminder_sent rows", async () => {
+ const id = await seedTaskReminderSentRow();
+ expect(await rowExists("task_reminder_sent", id)).toBe(true);
+
+ await resetTestDatabase();
+
+ expect(await rowExists("task_reminder_sent", id)).toBe(false);
+ });
+});
diff --git a/tests/api-integration/helpers/database.ts b/tests/api-integration/helpers/database.ts
index acd784629..1a8f9a0d5 100644
--- a/tests/api-integration/helpers/database.ts
+++ b/tests/api-integration/helpers/database.ts
@@ -3,39 +3,11 @@ import { fileURLToPath } from "node:url";
import { sql } from "drizzle-orm";
import { migrate } from "drizzle-orm/node-postgres/migrator";
import { Client } from "pg";
-import db, { schema } from "../../../apps/api/src/database";
+import db from "../../../apps/api/src/database";
const currentDir = dirname(fileURLToPath(import.meta.url));
const migrationsFolder = resolve(currentDir, "../../../apps/api/drizzle");
-const drizzleNameSymbol = Object.getOwnPropertySymbols(
- Object.values(schema).find((value) => value && typeof value === "object") ??
- {},
-).find((sym) => sym.toString() === "Symbol(drizzle:Name)");
-
-if (!drizzleNameSymbol) {
- throw new Error(
- "Could not locate Drizzle table name symbol; schema introspection changed",
- );
-}
-
-function getTruncateTableNames() {
- return Object.entries(schema)
- .filter(
- ([key, value]) =>
- key.endsWith("Table") &&
- !key.endsWith("TableRelations") &&
- value &&
- typeof value === "object" &&
- drizzleNameSymbol in value,
- )
- .map(
- ([, value]) =>
- (value as Record<symbol, unknown>)[drizzleNameSymbol] as string,
- )
- .sort();
-}
-
let migrationPromise: Promise<void> | null = null;
function getDatabaseName(connectionString: string) {
@@ -107,10 +79,33 @@ export async function ensureTestDatabaseMigrated() {
}
}
+// Ponytail: query Postgres directly. The catalog is the canonical source of
+// what tables actually exist after migrations run. Reflecting on the schema
+// object in apps/api/src/database misses any table that is not exported from
+// the index.ts registry (for example mcp_oauth_state and task_reminder_sent).
+async function listPublicTableNames(): Promise<string[]> {
+ const result = await db.execute<{ table_name: string }>(sql`
+ SELECT table_name
+ FROM information_schema.tables
+ WHERE table_schema = 'public'
+ AND table_type = 'BASE TABLE'
+ ORDER BY table_name
+ `);
+
+ return result.rows.map((row) => row.table_name);
+}
+
export async function resetTestDatabase() {
await ensureTestDatabaseMigrated();
- const tableNames = getTruncateTableNames();
+ const tableNames = await listPublicTableNames();
+
+ if (tableNames.length === 0) {
+ throw new Error(
+ "resetTestDatabase found no tables to truncate. Did migrations run?",
+ );
+ }
+
const formattedTableNames = tableNames.map((name) => `"${name}"`).join(", ");
await db.execute(