|
| 1 | +import { afterEach, beforeAll, beforeEach, expect, test } from "bun:test"; |
| 2 | +import type { StateValues } from "../../types/core"; |
| 3 | +import { describeIfSupabase } from "../../utils/__testHelpers__/integrationEnv"; |
| 4 | + |
| 5 | +describeIfSupabase("[integration] cleanValues strips legacy rawFiles", () => { |
| 6 | + // Dynamic imports — operations.ts instantiates the Supabase client at module |
| 7 | + // load time; loading it without env throws. |
| 8 | + let createState: typeof import("../../db/operations").createState; |
| 9 | + let getState: typeof import("../../db/operations").getState; |
| 10 | + let updateState: typeof import("../../db/operations").updateState; |
| 11 | + let getServiceClient: typeof import("../../db/client").getServiceClient; |
| 12 | + |
| 13 | + beforeAll(async () => { |
| 14 | + ({ createState, getState, updateState } = await import("../../db/operations")); |
| 15 | + ({ getServiceClient } = await import("../../db/client")); |
| 16 | + }); |
| 17 | + |
| 18 | + let stateId: string = ""; |
| 19 | + |
| 20 | + beforeEach(async () => { |
| 21 | + // Seed a row. createState does NOT run cleanValues, so it accepts the |
| 22 | + // legacy-shape rawFiles (not in the declared type) verbatim. |
| 23 | + const seed = { |
| 24 | + values: { |
| 25 | + rawFiles: [ |
| 26 | + { |
| 27 | + buffer: "base64-binary-stand-in-" + "A".repeat(2048), |
| 28 | + filename: "legacy.pdf", |
| 29 | + metadata: { pages: 3 }, |
| 30 | + mimeType: "application/pdf", |
| 31 | + parsedText: "a".repeat(10_000), |
| 32 | + }, |
| 33 | + ], |
| 34 | + } as unknown as StateValues, |
| 35 | + }; |
| 36 | + const row = await createState(seed); |
| 37 | + stateId = row.id!; |
| 38 | + |
| 39 | + // Verify the seed landed with the heavy fields intact — otherwise the |
| 40 | + // post-update assertions would pass vacuously if the JSONB column (or a |
| 41 | + // future schema change) silently coerced them away at insert time. |
| 42 | + const seeded = await getState(stateId); |
| 43 | + const seededRf = (seeded!.values as { rawFiles?: Array<Record<string, unknown>> }).rawFiles; |
| 44 | + expect(seededRf?.[0]?.buffer).toBeDefined(); |
| 45 | + expect(seededRf?.[0]?.parsedText).toBeDefined(); |
| 46 | + }); |
| 47 | + |
| 48 | + afterEach(async () => { |
| 49 | + if (!stateId) return; |
| 50 | + const supabase = getServiceClient(); |
| 51 | + const { error } = await supabase.from("states").delete().eq("id", stateId); |
| 52 | + if (error) throw new Error(`states cleanup failed: ${error.message}`); |
| 53 | + stateId = ""; |
| 54 | + }); |
| 55 | + |
| 56 | + test("updateState with rawFiles payload persists without buffer/parsedText", async () => { |
| 57 | + // Invariant: cleanValues must strip `buffer` and `parsedText` from every |
| 58 | + // rawFiles entry before persistence, so state rows don't balloon past |
| 59 | + // Postgres' JSONB row limits as files accumulate in a conversation. |
| 60 | + const rawFilesPayload = { |
| 61 | + rawFiles: [ |
| 62 | + { |
| 63 | + buffer: "base64-binary-stand-in-" + "B".repeat(2048), |
| 64 | + filename: "legacy.pdf", |
| 65 | + metadata: { pages: 3 }, |
| 66 | + mimeType: "application/pdf", |
| 67 | + parsedText: "b".repeat(10_000), |
| 68 | + }, |
| 69 | + ], |
| 70 | + } as unknown as Partial<StateValues>; |
| 71 | + |
| 72 | + await updateState(stateId, rawFilesPayload); |
| 73 | + |
| 74 | + const row = await getState(stateId); |
| 75 | + expect(row).toBeDefined(); |
| 76 | + |
| 77 | + const persisted = row!.values as Record<string, unknown>; |
| 78 | + const rf = persisted.rawFiles as Array<Record<string, unknown>> | undefined; |
| 79 | + expect(rf).toBeDefined(); |
| 80 | + expect(rf!).toHaveLength(1); |
| 81 | + |
| 82 | + const entry = rf![0]!; |
| 83 | + expect(entry.buffer).toBeUndefined(); |
| 84 | + expect(entry.parsedText).toBeUndefined(); |
| 85 | + expect(entry.filename).toBe("legacy.pdf"); |
| 86 | + expect(entry.mimeType).toBe("application/pdf"); |
| 87 | + expect(entry.metadata).toEqual({ pages: 3 }); |
| 88 | + }); |
| 89 | +}); |
0 commit comments