File size: 3,142 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { applyAvailableAt, grantAffiliatePro, parseApplication, validateSlug } from "./affiliates";

describe("validateSlug", () => {
  it.each(["maya", "Maya", "  maya-clips ", "abc", "a1b2c3d4e5f6g7h8i9j0"])("accepts %j", (slug) => {
    expect(validateSlug(slug)).toEqual({ ok: true, slug: slug.trim().toLowerCase() });
  });

  it.each([
    ["too short", "ab"],
    ["too long", "a".repeat(21)],
    ["leading hyphen", "-maya"],
    ["trailing hyphen", "maya-"],
    ["double hyphen", "ma--ya"],
    ["spaces", "ma ya"],
    ["symbols", "maya!"],
    ["reserved", "admin"],
    ["brand prefix", "supoclip-deals"],
    ["not a string", 42],
  ])("rejects %s", (_label, slug) => {
    expect(validateSlug(slug).ok).toBe(false);
  });
});

describe("parseApplication", () => {
  const valid = {
    slug: "Maya",
    platform: "tiktok",
    profile_url: "https://www.tiktok.com/@maya",
    audience_size: "1k-10k",
    video_url: "",
    promotion_plan: "  Weekly editing tips  ",
    accept_terms: true,
  };

  it("normalizes a valid application", () => {
    expect(parseApplication(valid)).toEqual({
      ok: true,
      application: {
        slug: "maya",
        platform: "tiktok",
        profile_url: "https://www.tiktok.com/@maya",
        audience_size: "1k-10k",
        video_url: null,
        promotion_plan: "Weekly editing tips",
      },
    });
  });

  it.each([
    ["unknown platform", { platform: "myspace" }],
    ["unknown audience size", { audience_size: "huge" }],
    ["missing profile", { profile_url: "" }],
    ["javascript profile link", { profile_url: "javascript:alert(1)" }],
    ["relative video link", { video_url: "/watch" }],
    ["terms not accepted", { accept_terms: "yes" }],
  ])("rejects %s", (_label, override) => {
    expect(parseApplication({ ...valid, ...override }).ok).toBe(false);
  });
});

describe("applyAvailableAt", () => {
  const now = new Date("2026-10-09T12:00:00Z");

  it("lets new applicants apply now", () => {
    expect(applyAvailableAt(null, now)).toEqual(now);
  });

  it.each(["pending", "approved", "revoked"])("blocks reapplying while %s", (status) => {
    expect(applyAvailableAt({ status, reviewed_at: now }, now)).toBeNull();
  });

  it("allows reapplying 30 days after a decline", () => {
    const declined = { status: "declined", reviewed_at: new Date("2026-10-01T12:00:00Z") };
    expect(applyAvailableAt(declined, now)).toEqual(new Date("2026-10-31T12:00:00Z"));
    expect(applyAvailableAt(declined, new Date("2026-11-05T00:00:00Z"))).toEqual(new Date("2026-11-05T00:00:00Z"));
  });
});

describe("grantAffiliatePro", () => {
  it("never overrides an active paid subscription", async () => {
    const updateMany = vi.fn().mockResolvedValue({ count: 1 });

    await grantAffiliatePro({ user: { updateMany } } as never, "user-1");

    expect(updateMany).toHaveBeenCalledWith({
      where: {
        id: "user-1",
        OR: [{ subscription_provider: "affiliate" }, { subscription_status: { notIn: ["active", "trialing"] } }],
      },
      data: { plan: "pro", subscription_status: "active", subscription_provider: "affiliate" },
    });
  });
});