cleanup-immunity.test.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { readFileSync } from "node:fs";
  2. import { resolve } from "node:path";
  3. import { describe, expect, it } from "vitest";
  4. const serviceTs = readFileSync(resolve(process.cwd(), "src/lib/log-cleanup/service.ts"), "utf-8");
  5. const usersTs = readFileSync(resolve(process.cwd(), "src/actions/users.ts"), "utf-8");
  6. describe("usage_ledger cleanup immunity", () => {
  7. it("log cleanup service never imports or queries usageLedger", () => {
  8. expect(serviceTs).not.toMatch(/import\b.*\busageLedger\b/);
  9. expect(serviceTs).not.toMatch(/from.*schema.*usageLedger/);
  10. expect(serviceTs).not.toContain("db.delete(usageLedger)");
  11. expect(serviceTs).not.toContain('from("usage_ledger")');
  12. expect(serviceTs).not.toContain("FROM usage_ledger");
  13. });
  14. it("removeUser does not delete from usageLedger", () => {
  15. const removeUserMatch = usersTs.match(/export async function removeUser[\s\S]*?^}/m);
  16. expect(removeUserMatch).not.toBeNull();
  17. const removeUserBody = removeUserMatch![0];
  18. expect(removeUserBody).not.toContain("db.delete(usageLedger)");
  19. });
  20. it("resetUserAllStatistics deletes from both tables", () => {
  21. const resetMatch = usersTs.match(/export async function resetUserAllStatistics[\s\S]*?^}/m);
  22. expect(resetMatch).not.toBeNull();
  23. const resetBody = resetMatch![0];
  24. expect(resetBody).toContain("db.delete(messageRequest)");
  25. expect(resetBody).toContain("db.delete(usageLedger)");
  26. });
  27. it("resetUserAllStatistics is the only usageLedger delete path in users.ts", () => {
  28. const allDeleteMatches = [...usersTs.matchAll(/db\.delete\(usageLedger\)/g)];
  29. expect(allDeleteMatches).toHaveLength(1);
  30. const deleteIndex = usersTs.indexOf("db.delete(usageLedger)");
  31. const precedingChunk = usersTs.slice(Math.max(0, deleteIndex - 2000), deleteIndex);
  32. expect(precedingChunk).toContain("resetUserAllStatistics");
  33. });
  34. });