File size: 2,007 Bytes
689359a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { claimAnonymousAttribution, captureAnonymousAttribution, confirmAccountAttribution } from "./attribution";
import { trackClipExport } from "./clip-export-tracking";
import { track } from "./datafast";

vi.mock("./datafast", () => ({
  track: vi.fn(),
}));

describe("trackClipExport", () => {
  const storage = new Map<string, string>();
  const fetchMock = vi.fn();

  beforeEach(() => {
    storage.clear();
    vi.mocked(track).mockReset();
    vi.stubGlobal("localStorage", {
      getItem: (key: string) => storage.get(key) ?? null,
      setItem: (key: string, value: string) => storage.set(key, value),
      removeItem: (key: string) => storage.delete(key),
    });
    fetchMock.mockReset().mockResolvedValue(Response.json({ first_export: true, attribution: null, hours_since_signup: 1 }));
    vi.stubGlobal("fetch", fetchMock);
  });

  afterEach(() => {
    vi.unstubAllGlobals();
  });

  it("attributes exports to the claimed visit while the sync is pending", async () => {
    captureAnonymousAttribution("https://www.supoclip.com/?utm_source=reddit&utm_campaign=launch", "");
    claimAnonymousAttribution("user-a");

    await trackClipExport({ userId: "user-a", surface: "task", format: "tiktok" });

    expect(track).toHaveBeenCalledWith("clip_exported", expect.objectContaining({ utm_source: "reddit", utm_campaign: "launch" }));
    expect(track).toHaveBeenCalledWith("first_clip_exported", expect.objectContaining({ utm_source: "reddit", hours_since_signup: 1 }));
  });

  it("never attaches another account's attribution", async () => {
    captureAnonymousAttribution("https://www.supoclip.com/?utm_source=reddit", "");
    claimAnonymousAttribution("user-a");
    confirmAccountAttribution("user-c", { utm_source: "newsletter", captured_at: "2026-10-05T10:00:00.000Z" }, null);

    await trackClipExport({ userId: "user-b", surface: "editor", format: "reels" });

    expect(track).toHaveBeenCalledWith("clip_exported", { surface: "editor", format: "reels" });
  });
});