File size: 7,288 Bytes
56838f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import { convexTest } from "convex-test";
import { afterEach, describe, expect, test, vi } from "vitest";
import { internal } from "../_generated/api";
import schema from "../schema";

const modules = import.meta.glob("../**/*.ts");
const noticeFns = (internal as any).apiPlanLimitNotices;
const emailFns = (internal as any).apiPlanLimitEmails;

const NOW = 1_800_000_000_000;
const originalFetch = globalThis.fetch;
const originalResend = process.env.RESEND_API_KEY;

function restoreEnv() {
  if (originalResend === undefined) delete process.env.RESEND_API_KEY;
  else process.env.RESEND_API_KEY = originalResend;
}

async function seedNotice(t: ReturnType<typeof convexTest>, userId = "user-api") {
  return await t.mutation(noticeFns.recordUsageEvaluation, {
    rollup: {
      userId,
      planKey: "api_starter",
      dimension: "api_daily_requests",
      windowKey: "2026-07-02",
      windowStart: NOW,
      windowEnd: NOW + 86_400_000,
      limit: 1_000,
      usage: 1_200,
      source: "test",
      sourceFreshAt: NOW,
      computedAt: NOW,
    },
    notice: {
      state: "over_limit",
      upgradeTargetPlanKey: "api_business",
      ctaKind: "contact_support",
      blockedReason: "api_business_not_self_serve",
    },
  });
}

afterEach(() => {
  globalThis.fetch = originalFetch;
  restoreEnv();
  vi.restoreAllMocks();
});

describe("api plan-limit email delivery", () => {
  test("sends due notice and records sent status", async () => {
    const t = convexTest(schema, modules);
    await seedNotice(t);
    await t.run(async (ctx) => {
      await ctx.db.insert("customers", {
        userId: "user-api",
        email: "Owner@Example.com",
        normalizedEmail: "owner@example.com",
        createdAt: NOW,
        updatedAt: NOW,
      });
    });

    process.env.RESEND_API_KEY = "resend-test";
    const calls: unknown[] = [];
    globalThis.fetch = (async (_url, init) => {
      calls.push(JSON.parse(String(init?.body)));
      return new Response(JSON.stringify({ id: "email_1" }), { status: 200 });
    }) as typeof fetch;

    const summary = await t.action(emailFns.sendDuePlanLimitEmails, { now: NOW + 1_000, live: true });
    expect(summary).toMatchObject({ considered: 1, sent: 1, skipped: 0, failed: 0 });
    expect(calls).toHaveLength(1);
    expect(calls[0]).toMatchObject({
      to: ["owner@example.com"],
      subject: "World Monitor usage notice: daily API requests exceeded plan limit",
    });

    const notices = await t.run((ctx) => ctx.db.query("apiPlanLimitNotices").collect());
    expect(notices[0].emailStatus).toBe("sent");
    expect(notices[0].lastEmailedAt).toBe(NOW + 1_000);
  });

  test("dry-run kill-switch (no live flag) sends nothing and leaves notices pending", async () => {
    const t = convexTest(schema, modules);
    await seedNotice(t);
    await t.run(async (ctx) => {
      await ctx.db.insert("customers", {
        userId: "user-api",
        email: "owner@example.com",
        normalizedEmail: "owner@example.com",
        createdAt: NOW,
        updatedAt: NOW,
      });
    });
    // Even with a Resend key and a due, deliverable notice, the default run must
    // NOT send: PLAN_LIMIT_NOTIFY_LIVE is unset and no `live` arg is passed.
    process.env.RESEND_API_KEY = "resend-test";
    delete process.env.PLAN_LIMIT_NOTIFY_LIVE;
    const fetchMock = vi.fn();
    globalThis.fetch = fetchMock as unknown as typeof fetch;

    const summary = await t.action(emailFns.sendDuePlanLimitEmails, { now: NOW + 1_000 });
    expect(summary).toMatchObject({ considered: 1, sent: 0, skipped: 0, failed: 0, dryRun: true });
    expect(fetchMock).not.toHaveBeenCalled();

    const notices = await t.run((ctx) => ctx.db.query("apiPlanLimitNotices").collect());
    expect(notices[0]).toMatchObject({ emailStatus: "pending", current: true });
  });

  test("suppressed recipient marks notice suppressed without calling Resend", async () => {
    const t = convexTest(schema, modules);
    await seedNotice(t);
    await t.run(async (ctx) => {
      await ctx.db.insert("customers", {
        userId: "user-api",
        email: "owner@example.com",
        normalizedEmail: "owner@example.com",
        createdAt: NOW,
        updatedAt: NOW,
      });
      await ctx.db.insert("emailSuppressions", {
        normalizedEmail: "owner@example.com",
        reason: "bounce",
        suppressedAt: NOW,
      });
    });
    process.env.RESEND_API_KEY = "resend-test";
    globalThis.fetch = vi.fn() as unknown as typeof fetch;

    const summary = await t.action(emailFns.sendDuePlanLimitEmails, { now: NOW + 1_000, live: true });
    expect(summary).toMatchObject({ considered: 1, sent: 0, skipped: 1, failed: 0 });
    expect(globalThis.fetch).not.toHaveBeenCalled();

    const notices = await t.run((ctx) => ctx.db.query("apiPlanLimitNotices").collect());
    expect(notices[0].emailStatus).toBe("suppressed");
  });

  test("missing recipient marks notice skipped and leaves in-app notice current", async () => {
    const t = convexTest(schema, modules);
    await seedNotice(t);
    process.env.RESEND_API_KEY = "resend-test";

    const summary = await t.action(emailFns.sendDuePlanLimitEmails, { now: NOW + 1_000, live: true });
    expect(summary).toMatchObject({ considered: 1, sent: 0, skipped: 1, failed: 0 });

    const notices = await t.run((ctx) => ctx.db.query("apiPlanLimitNotices").collect());
    expect(notices[0]).toMatchObject({ emailStatus: "skipped", current: true });
  });

  test("continues through Resend failures before surfacing the batch error", async () => {
    const t = convexTest(schema, modules);
    await seedNotice(t, "user-api-1");
    await seedNotice(t, "user-api-2");
    await t.run(async (ctx) => {
      await ctx.db.insert("customers", {
        userId: "user-api-1",
        email: "one@example.com",
        normalizedEmail: "one@example.com",
        createdAt: NOW,
        updatedAt: NOW,
      });
      await ctx.db.insert("customers", {
        userId: "user-api-2",
        email: "two@example.com",
        normalizedEmail: "two@example.com",
        createdAt: NOW,
        updatedAt: NOW,
      });
    });

    process.env.RESEND_API_KEY = "resend-test";
    const calls: string[] = [];
    globalThis.fetch = (async (_url, init) => {
      const body = JSON.parse(String(init?.body));
      calls.push(body.to[0]);
      if (calls.length === 1) {
        return new Response("temporary upstream error", { status: 503 });
      }
      return new Response(JSON.stringify({ id: "email_2" }), { status: 200 });
    }) as typeof fetch;

    await expect(t.action(emailFns.sendDuePlanLimitEmails, { now: NOW + 1_000, live: true }))
      .rejects
      .toThrow("[apiPlanLimitEmails] 1 email delivery failure");
    expect(calls).toEqual(["one@example.com", "two@example.com"]);

    const notices = await t.run((ctx) => ctx.db.query("apiPlanLimitNotices").collect());
    const failedNotice = notices.find((notice) => notice.userId === "user-api-1");
    expect(failedNotice).toMatchObject({ emailStatus: "failed" });
    expect(failedNotice?.lastEmailedAt).toBeUndefined();
    expect(notices.find((notice) => notice.userId === "user-api-2")).toMatchObject({
      emailStatus: "sent",
      lastEmailedAt: NOW + 1_000,
    });
  });
});