import fs from "node:fs"; import path from "node:path"; import { describe, expect, it } from "vitest"; const rowsApiPath = path.resolve(process.cwd(), "apps/web/app/utils/api/.server/RowsApi.ts"); describe("RowsApi workflow outbox transaction boundary", () => { it("creates rows, reloads rows, and records row.created workflow events inside the same transaction", () => { const source = fs.readFileSync(rowsApiPath, "utf8"); const createSection = source.slice(source.indexOf("export async function create({"), source.indexOf("export async function createSimple({")); expect(createSection).toContain("db.$transaction(async (tx) =>"); expect(createSection).toContain("const rowClient = tx as unknown as RowCreateClient"); expect(createSection).toContain("rowClient\n );"); expect(createSection).toContain("getLegacyRowById(item.id, rowClient, tenantId)"); expect(createSection).toContain("EventsService.createInTransaction(tx as Parameters[0]"); expect(createSection).toContain("EventsService.dispatchPostCommitSideEffects(rowCreatedEvent)"); expect(createSection).not.toContain('EventsService.create({\n request,\n event: "row.created"'); }); it("updates rows and records row.updated workflow events inside the same transaction", () => { const source = fs.readFileSync(rowsApiPath, "utf8"); const updateSection = source.slice(source.indexOf("export async function update("), source.indexOf("export async function del(")); expect(updateSection).toContain("db.$transaction(async (tx) =>"); expect(updateSection).toContain("updateRowWithValuesInTransaction("); expect(updateSection).toContain('event: "row.updated"'); expect(updateSection).toContain("EventsService.createInTransaction(tx as Parameters[0]"); expect(updateSection.indexOf("updateRowWithValuesInTransaction(")).toBeLessThan(updateSection.indexOf("EventsService.createInTransaction")); expect(updateSection).toContain("EventsService.dispatchPostCommitSideEffects(rowUpdatedEvent)"); expect(updateSection).not.toContain('EventsService.create({\n request,\n event: "row.updated"'); }); it("deletes rows and records row.deleted workflow events inside the same transaction", () => { const source = fs.readFileSync(rowsApiPath, "utf8"); const deleteSection = source.slice(source.indexOf("export async function del("), source.indexOf("export async function count({")); expect(deleteSection).toContain("db.$transaction(async (tx) =>"); expect(deleteSection).toContain("hardDeleteRowById({ id, entityId: rowEntity.id, tenantId: row.tenantId ?? null }, tx as unknown as RowDeleteClient)"); expect(deleteSection).toContain('event: "row.deleted"'); expect(deleteSection).toContain("EventsService.createInTransaction(tx as Parameters[0]"); expect(deleteSection.indexOf("hardDeleteRowById(")).toBeLessThan(deleteSection.indexOf("EventsService.createInTransaction")); expect(deleteSection).toContain("EventsService.dispatchPostCommitSideEffects(rowDeletedEvent)"); expect(deleteSection).not.toContain('EventsService.create({\n request,\n event: "row.deleted"'); }); });