diff --git a/server/src/__tests__/company-skills-list-statements.test.ts b/server/src/__tests__/company-skills-list-statements.test.ts new file mode 100644 index 00000000000..848ffc7a456 --- /dev/null +++ b/server/src/__tests__/company-skills-list-statements.test.ts @@ -0,0 +1,104 @@ +import request from "supertest"; +import { and, eq, like } from "drizzle-orm"; +import { expect, it } from "vitest"; +import { folders } from "@paperclipai/db"; +import { companySkillRoutes } from "../routes/company-skills.js"; +import { folderService } from "../services/folders.js"; +import { meterDatabaseStatements } from "./helpers/database-statement-meter.js"; +import { describeEmbeddedPostgres, routeApp, seedCompanyWithBoardAccess, useEmbeddedPostgres } from "./helpers/route-test-harness.js"; + +describeEmbeddedPostgres("GET /api/companies/:companyId/skills statements per request", () => { + const ctx = useEmbeddedPostgres("company-skills-list-statements-"); + + function meter() { + const { host, port } = (ctx.db.$client as unknown as { options: { host: string[]; port: number[] } }).options; + return meterDatabaseStatements(ctx.db, `postgres://${host[0]}:${port[0]}/skills`); + } + + const bundledFolders = (companyId: string) => ctx.db.select().from(folders) + .where(and(eq(folders.companyId, companyId), like(folders.systemKey, "bundled%"))); + + it("lists skills with plain reads once the bundled folders exist", async () => { + const statements = meter(); + try { + const company = await seedCompanyWithBoardAccess(ctx.db, "Skills list"); + const app = routeApp(ctx.db, company.actor, (db) => companySkillRoutes(db)); + const first = await request(app).get(`/api/companies/${company.companyId}/skills`).expect(200); + expect(first.body.length).toBeGreaterThan(0); + const foldersAfterFirst = await bundledFolders(company.companyId); + + const before = statements.read(); + statements.startLog(); + const second = await request(app).get(`/api/companies/${company.companyId}/skills`).expect(200); + const log = statements.stopLog(); + const after = statements.read(); + + expect(second.body).toEqual(first.body); + expect(after.transactions - before.transactions).toBe(0); + expect(log.filter((entry) => /pg_advisory/.test(entry.sql))).toEqual([]); + expect(after.statements - before.statements).toBeLessThanOrEqual(45); + expect(await bundledFolders(company.companyId)).toEqual(foldersAfterFirst); + } finally { + statements.uninstall(); + } + }, 60_000); + + it("creates exactly one folder when two first requests ensure the same category at once", async () => { + const company = await seedCompanyWithBoardAccess(ctx.db, "Skills race"); + const [left, right] = await Promise.all([ + folderService(ctx.db).ensureBundledCategory(company.companyId, "Race Category"), + folderService(ctx.db).ensureBundledCategory(company.companyId, "Race Category"), + ]); + expect(left.id).toBe(right.id); + const rows = await bundledFolders(company.companyId); + expect(rows.filter((row) => row.systemKey === "bundled:race-category")).toHaveLength(1); + expect(rows.filter((row) => row.systemKey === "bundled")).toHaveLength(1); + + const app = routeApp(ctx.db, company.actor, (db) => companySkillRoutes(db)); + await Promise.all([ + request(app).get(`/api/companies/${company.companyId}/skills`).expect(200), + request(app).get(`/api/companies/${company.companyId}/skills`).expect(200), + ]); + const systemKeys = (await bundledFolders(company.companyId)).map((row) => row.systemKey); + expect(new Set(systemKeys).size).toBe(systemKeys.length); + }, 60_000); + + it("still renames a changed category and prunes an empty one under the lock", async () => { + const statements = meter(); + try { + const company = await seedCompanyWithBoardAccess(ctx.db, "Skills repair"); + const svc = folderService(ctx.db); + const locked = async (write: () => Promise) => { + const before = statements.read(); + statements.startLog(); + await write(); + const log = statements.stopLog(); + const lockIndex = log.findIndex((entry) => /pg_advisory_xact_lock/.test(entry.sql)); + const writes = log.filter((entry) => /^\s*(insert|update|delete)\b/i.test(entry.sql)); + return { + transactions: statements.read().transactions - before.transactions, + lockedBeforeWrite: lockIndex >= 0 && writes.every((entry) => entry.inTransaction && log.indexOf(entry) > lockIndex), + writes: writes.length, + }; + }; + + const created = await locked(() => svc.ensureBundledCategory(company.companyId, "Repair Category")); + expect(created).toMatchObject({ transactions: 1, lockedBeforeWrite: true }); + expect(created.writes).toBeGreaterThan(0); + const [category] = (await bundledFolders(company.companyId)).filter((row) => row.systemKey === "bundled:repair-category"); + await ctx.db.update(folders).set({ name: "Renamed by hand" }).where(eq(folders.id, category!.id)); + const renamed = await locked(() => svc.ensureBundledCategory(company.companyId, "Repair Category")); + expect(renamed).toMatchObject({ transactions: 1, lockedBeforeWrite: true, writes: 1 }); + expect((await bundledFolders(company.companyId)).find((row) => row.id === category!.id)?.name).toBe("Repair Category"); + + expect(await locked(() => svc.pruneEmptyBundledCategories(company.companyId, ["Repair Category"]))) + .toEqual({ transactions: 0, lockedBeforeWrite: false, writes: 0 }); + expect((await bundledFolders(company.companyId)).map((row) => row.id)).toContain(category!.id); + expect(await locked(() => svc.pruneEmptyBundledCategories(company.companyId, []))) + .toEqual({ transactions: 1, lockedBeforeWrite: true, writes: 1 }); + expect((await bundledFolders(company.companyId)).map((row) => row.id)).not.toContain(category!.id); + } finally { + statements.uninstall(); + } + }, 60_000); +}); diff --git a/server/src/services/folders.ts b/server/src/services/folders.ts index b16f6ac5c67..d62c3ad1601 100644 --- a/server/src/services/folders.ts +++ b/server/src/services/folders.ts @@ -544,14 +544,23 @@ export function folderService(db: Db, mutationLockHeld = false) { throw conflict("Could not create project skill folder"); } + async function findCurrentBundledCategory(companyId: string, systemKey: string, name: string) { + const rows = await getRows(companyId, "skill"); + const existing = rows.find((row) => row.systemKey === systemKey); + if (!existing || existing.name !== name || !rows.some((row) => row.systemKey === "bundled")) return null; + return buildFolderViews(rows).get(existing.id) ?? null; + } + async function ensureBundledCategory(companyId: string, category: string): Promise { - if (!mutationLockHeld) { - return withCompanyFolderLock(companyId, (lockedDb) => folderService(lockedDb, true).ensureBundledCategory(companyId, category)); - } - const root = await ensureContainer(companyId, "bundled", "Bundled"); const name = normalizeName(category); const slug = normalizeFolderSlug(category); const systemKey = `bundled:${slug}`; + if (!mutationLockHeld) { + const current = await findCurrentBundledCategory(companyId, systemKey, name); + if (current) return current; + return withCompanyFolderLock(companyId, (lockedDb) => folderService(lockedDb, true).ensureBundledCategory(companyId, category)); + } + const root = await ensureContainer(companyId, "bundled", "Bundled"); for (let attempt = 0; attempt < 3; attempt += 1) { const resolved = await uniqueSystemSlug(companyId, root.id, slug, systemKey, "bundled"); if (resolved.id) { @@ -576,16 +585,15 @@ export function folderService(db: Db, mutationLockHeld = false) { throw conflict("Could not create bundled skill folder"); } - async function pruneEmptyBundledCategories(companyId: string, retainedCategories: string[]): Promise { - if (!mutationLockHeld) { - return withCompanyFolderLock(companyId, (lockedDb) => folderService(lockedDb, true).pruneEmptyBundledCategories(companyId, retainedCategories)); - } - const root = await findSystemFolder(companyId, "bundled"); - if (!root) return; - const rows = await getRows(companyId, "skill"); + async function emptyBundledCategoryIds(companyId: string, retainedCategories: string[], rows: FolderRow[]) { + const root = rows.find((row) => row.kind === "skill" && row.systemKey === "bundled"); + if (!root) return []; const retainedSystemKeys = new Set( retainedCategories.map((category) => `bundled:${normalizeFolderSlug(category)}`), ); + const candidates = rows.filter((row) => row.parentId === root.id && row.systemKey?.startsWith("bundled:") + && !retainedSystemKeys.has(row.systemKey)); + if (candidates.length === 0) return []; const usedFolderIds = new Set( await db .select({ folderId: companySkills.folderId }) @@ -594,10 +602,16 @@ export function folderService(db: Db, mutationLockHeld = false) { .then((skills) => skills.flatMap((skill) => skill.folderId ? [skill.folderId] : [])), ); const parentIds = new Set(rows.flatMap((row) => row.parentId ? [row.parentId] : [])); - for (const row of rows) { - if (row.parentId !== root.id || !row.systemKey?.startsWith("bundled:")) continue; - if (retainedSystemKeys.has(row.systemKey) || usedFolderIds.has(row.id) || parentIds.has(row.id)) continue; - await db.delete(folders).where(and(eq(folders.companyId, companyId), eq(folders.id, row.id))); + return candidates.filter((row) => !usedFolderIds.has(row.id) && !parentIds.has(row.id)).map((row) => row.id); + } + + async function pruneEmptyBundledCategories(companyId: string, retainedCategories: string[]): Promise { + if (!mutationLockHeld) { + if ((await emptyBundledCategoryIds(companyId, retainedCategories, await getRows(companyId, "skill"))).length === 0) return; + return withCompanyFolderLock(companyId, (lockedDb) => folderService(lockedDb, true).pruneEmptyBundledCategories(companyId, retainedCategories)); + } + for (const id of await emptyBundledCategoryIds(companyId, retainedCategories, await getRows(companyId, "skill"))) { + await db.delete(folders).where(and(eq(folders.companyId, companyId), eq(folders.id, id))); } }