import { describe, expect, it } from "bun:test"; import { assertNoSegmentExternalIdsInValueArgument } from "./external-id-argument-guards"; const segmentExternalIds = [ { id: "gid://shopify/Customer/123", type: "shopify_customer_id", collection: "users" as const, encoding: "none" as const, }, ]; describe("external ID argument guards", () => { it("rejects own Segment-shaped externalIds in value arguments", () => { expect(() => assertNoSegmentExternalIdsInValueArgument({ argument: { plan: "pro", externalIds: segmentExternalIds }, commandName: "identify", valueArgumentName: "traits", callShape: "identify(userId, traits, { externalIds })", }), ).toThrow( "identify received externalIds in the traits argument. Use identify(userId, traits, { externalIds }) instead.", ); }); it("reads external ID fields from get-only proxies", () => { const externalId = new Proxy( {}, { get: (_target, property) => { switch (property) { case "id": return "gid://shopify/Customer/123"; case "type": return "shopify_customer_id"; case "collection": return "users"; case "encoding": return "none"; default: return undefined; } }, has: () => false, }, ); expect(() => assertNoSegmentExternalIdsInValueArgument({ argument: { externalIds: [externalId] }, commandName: "identify", valueArgumentName: "traits", callShape: "identify(userId, traits, { externalIds })", }), ).toThrow(); }); it("ignores inherited Segment-shaped externalIds", () => { const argument = Object.create({ externalIds: segmentExternalIds, }) as Record; argument.plan = "pro"; expect(() => assertNoSegmentExternalIdsInValueArgument({ argument, commandName: "identify", valueArgumentName: "traits", callShape: "identify(userId, traits, { externalIds })", }), ).not.toThrow(); }); });