import { describe, it, expect } from "vitest"; import { getTableName, getTableColumns } from "drizzle-orm"; import { forums, categories, users, memberships, posts, modActions, } from "../schema.js"; describe("database schema", () => { describe("forums table", () => { it("has the correct table name", () => { expect(getTableName(forums)).toBe("forums"); }); it("has expected columns", () => { const cols = getTableColumns(forums); expect(cols).toHaveProperty("id"); expect(cols).toHaveProperty("did"); expect(cols).toHaveProperty("rkey"); expect(cols).toHaveProperty("cid"); expect(cols).toHaveProperty("name"); expect(cols).toHaveProperty("description"); expect(cols).toHaveProperty("indexedAt"); }); it("has did and rkey as not-null", () => { const cols = getTableColumns(forums); expect(cols.did.notNull).toBe(true); expect(cols.rkey.notNull).toBe(true); }); }); describe("categories table", () => { it("has the correct table name", () => { expect(getTableName(categories)).toBe("categories"); }); it("has expected columns", () => { const cols = getTableColumns(categories); expect(cols).toHaveProperty("id"); expect(cols).toHaveProperty("did"); expect(cols).toHaveProperty("rkey"); expect(cols).toHaveProperty("cid"); expect(cols).toHaveProperty("name"); expect(cols).toHaveProperty("description"); expect(cols).toHaveProperty("slug"); expect(cols).toHaveProperty("sortOrder"); expect(cols).toHaveProperty("forumId"); expect(cols).toHaveProperty("createdAt"); expect(cols).toHaveProperty("indexedAt"); }); }); describe("users table", () => { it("has the correct table name", () => { expect(getTableName(users)).toBe("users"); }); it("uses did as primary key", () => { const cols = getTableColumns(users); expect(cols.did.primary).toBe(true); }); it("has handle as optional", () => { const cols = getTableColumns(users); expect(cols.handle.notNull).toBe(false); }); }); describe("memberships table", () => { it("has the correct table name", () => { expect(getTableName(memberships)).toBe("memberships"); }); it("has expected columns", () => { const cols = getTableColumns(memberships); expect(cols).toHaveProperty("id"); expect(cols).toHaveProperty("did"); expect(cols).toHaveProperty("rkey"); expect(cols).toHaveProperty("cid"); expect(cols).toHaveProperty("forumId"); expect(cols).toHaveProperty("forumUri"); expect(cols).toHaveProperty("role"); expect(cols).toHaveProperty("roleUri"); expect(cols).toHaveProperty("joinedAt"); expect(cols).toHaveProperty("createdAt"); expect(cols).toHaveProperty("indexedAt"); }); it("has did and forumUri as not-null", () => { const cols = getTableColumns(memberships); expect(cols.did.notNull).toBe(true); expect(cols.forumUri.notNull).toBe(true); }); }); describe("posts table", () => { it("has the correct table name", () => { expect(getTableName(posts)).toBe("posts"); }); it("has expected columns for the unified post model", () => { const cols = getTableColumns(posts); expect(cols).toHaveProperty("id"); expect(cols).toHaveProperty("did"); expect(cols).toHaveProperty("rkey"); expect(cols).toHaveProperty("cid"); expect(cols).toHaveProperty("text"); expect(cols).toHaveProperty("forumUri"); expect(cols).toHaveProperty("rootPostId"); expect(cols).toHaveProperty("parentPostId"); expect(cols).toHaveProperty("rootUri"); expect(cols).toHaveProperty("parentUri"); expect(cols).toHaveProperty("createdAt"); expect(cols).toHaveProperty("indexedAt"); expect(cols).toHaveProperty("bannedByMod"); expect(cols).toHaveProperty("deletedByUser"); }); it("has text as not-null", () => { const cols = getTableColumns(posts); expect(cols.text.notNull).toBe(true); }); it("has bannedByMod defaulting to false", () => { const cols = getTableColumns(posts); expect(cols.bannedByMod.notNull).toBe(true); expect(cols.bannedByMod.hasDefault).toBe(true); }); it("has deletedByUser defaulting to false", () => { const cols = getTableColumns(posts); expect(cols.deletedByUser.notNull).toBe(true); expect(cols.deletedByUser.hasDefault).toBe(true); }); it("has rootPostId and parentPostId as nullable (topics have no parent)", () => { const cols = getTableColumns(posts); expect(cols.rootPostId.notNull).toBe(false); expect(cols.parentPostId.notNull).toBe(false); }); }); describe("modActions table", () => { it("has the correct table name", () => { expect(getTableName(modActions)).toBe("mod_actions"); }); it("has expected columns", () => { const cols = getTableColumns(modActions); expect(cols).toHaveProperty("id"); expect(cols).toHaveProperty("did"); expect(cols).toHaveProperty("rkey"); expect(cols).toHaveProperty("cid"); expect(cols).toHaveProperty("action"); expect(cols).toHaveProperty("subjectDid"); expect(cols).toHaveProperty("subjectPostUri"); expect(cols).toHaveProperty("forumId"); expect(cols).toHaveProperty("reason"); expect(cols).toHaveProperty("createdBy"); expect(cols).toHaveProperty("expiresAt"); expect(cols).toHaveProperty("createdAt"); expect(cols).toHaveProperty("indexedAt"); }); it("has action and createdBy as not-null", () => { const cols = getTableColumns(modActions); expect(cols.action.notNull).toBe(true); expect(cols.createdBy.notNull).toBe(true); }); }); describe("all tables export correctly", () => { it("exports six tables", () => { const tables = [forums, categories, users, memberships, posts, modActions]; expect(tables).toHaveLength(6); tables.forEach((table) => { expect(table).toBeDefined(); }); }); }); });