|
1 | 1 | import { describe, expect, it } from "vitest"; |
2 | | -import { countUserMessages, FREE_PLAN_MESSAGE_LIMIT, resolveMessageQuota } from "./message-quota"; |
| 2 | +import { |
| 3 | + countUserMessages, |
| 4 | + FREE_PLAN_MESSAGE_LIMIT, |
| 5 | + MESSAGE_QUOTA_REACHED_ERROR, |
| 6 | + messageQuotaReachedCopy, |
| 7 | + parseQuotaReachedResponse, |
| 8 | + resolveMessageQuota, |
| 9 | +} from "./message-quota"; |
3 | 10 |
|
4 | 11 | describe("resolveMessageQuota", () => { |
5 | 12 | it("caps a Free plan at the limit", () => { |
@@ -42,6 +49,37 @@ describe("resolveMessageQuota", () => { |
42 | 49 | }); |
43 | 50 | }); |
44 | 51 |
|
| 52 | +describe("parseQuotaReachedResponse", () => { |
| 53 | + it("maps a create/in 403 cap body to the limit", () => { |
| 54 | + // Both the create path and the `in` transport refuse with this exact body. |
| 55 | + expect( |
| 56 | + parseQuotaReachedResponse(403, { error: MESSAGE_QUOTA_REACHED_ERROR, limit: 20 }) |
| 57 | + ).toEqual({ limit: 20 }); |
| 58 | + }); |
| 59 | + |
| 60 | + it("falls back to the free limit when the body omits it", () => { |
| 61 | + expect(parseQuotaReachedResponse(403, { error: MESSAGE_QUOTA_REACHED_ERROR })).toEqual({ |
| 62 | + limit: FREE_PLAN_MESSAGE_LIMIT, |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + it("ignores other errors and non-403 statuses so they surface normally", () => { |
| 67 | + expect(parseQuotaReachedResponse(403, { error: "something_else" })).toBeNull(); |
| 68 | + expect(parseQuotaReachedResponse(500, { error: MESSAGE_QUOTA_REACHED_ERROR })).toBeNull(); |
| 69 | + expect(parseQuotaReachedResponse(403, null)).toBeNull(); |
| 70 | + }); |
| 71 | +}); |
| 72 | + |
| 73 | +describe("messageQuotaReachedCopy", () => { |
| 74 | + it("is a friendly sentence naming the limit, never the raw code", () => { |
| 75 | + const copy = messageQuotaReachedCopy(20); |
| 76 | + expect(copy).toContain("all 20 messages"); |
| 77 | + expect(copy).toContain("Free plan"); |
| 78 | + // Control break: if the mapping leaked the server code, this fails. |
| 79 | + expect(copy).not.toContain(MESSAGE_QUOTA_REACHED_ERROR); |
| 80 | + }); |
| 81 | +}); |
| 82 | + |
45 | 83 | describe("countUserMessages", () => { |
46 | 84 | it("counts only what the user sent", () => { |
47 | 85 | expect( |
|
0 commit comments