| 1 |
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 |
import { |
| 3 |
UnauthorizedError, |
| 4 |
UserExistsError, |
| 5 |
TopicReservedError, |
| 6 |
AccountActionLimitReachedError, |
| 7 |
IncorrectPasswordError, |
| 8 |
EmailVerificationCodeInvalidError, |
| 9 |
EmailPrimaryElsewhereError, |
| 10 |
throwAppError, |
| 11 |
fetchOrThrow, |
| 12 |
} from "./errors"; |
| 13 |
|
| 14 |
// A minimal stand-in for a fetch Response: just the bits errors.js reads. |
| 15 |
const fakeResponse = (status, body) => ({ |
| 16 |
status, |
| 17 |
json: async () => { |
| 18 |
if (body === undefined) throw new Error("no body"); |
| 19 |
return body; |
| 20 |
}, |
| 21 |
}); |
| 22 |
|
| 23 |
beforeEach(() => { |
| 24 |
// errors.js logs to console.log on every error path; keep test output clean. |
| 25 |
vi.spyOn(console, "log").mockImplementation(() => {}); |
| 26 |
}); |
| 27 |
|
| 28 |
afterEach(() => { |
| 29 |
vi.restoreAllMocks(); |
| 30 |
}); |
| 31 |
|
| 32 |
describe("error classes", () => { |
| 33 |
it("carry the server error codes from errors.go", () => { |
| 34 |
expect(UserExistsError.CODE).toBe(40901); |
| 35 |
expect(TopicReservedError.CODE).toBe(40902); |
| 36 |
expect(AccountActionLimitReachedError.CODE).toBe(42906); |
| 37 |
expect(IncorrectPasswordError.CODE).toBe(40026); |
| 38 |
expect(EmailVerificationCodeInvalidError.CODE).toBe(40051); |
| 39 |
expect(EmailPrimaryElsewhereError.CODE).toBe(40908); |
| 40 |
}); |
| 41 |
|
| 42 |
it("are Error subclasses with human-readable messages", () => { |
| 43 |
expect(new UnauthorizedError()).toBeInstanceOf(Error); |
| 44 |
expect(new UnauthorizedError().message).toBe("Unauthorized"); |
| 45 |
expect(new UserExistsError().message).toBe("Username already exists"); |
| 46 |
}); |
| 47 |
}); |
| 48 |
|
| 49 |
describe("throwAppError", () => { |
| 50 |
it("maps 401 and 403 to UnauthorizedError", async () => { |
| 51 |
await expect(throwAppError(fakeResponse(401))).rejects.toBeInstanceOf(UnauthorizedError); |
| 52 |
await expect(throwAppError(fakeResponse(403))).rejects.toBeInstanceOf(UnauthorizedError); |
| 53 |
}); |
| 54 |
|
| 55 |
it("maps known ntfy error codes to their specific error classes", async () => { |
| 56 |
await expect(throwAppError(fakeResponse(409, { code: UserExistsError.CODE }))).rejects.toBeInstanceOf(UserExistsError); |
| 57 |
await expect(throwAppError(fakeResponse(409, { code: TopicReservedError.CODE }))).rejects.toBeInstanceOf(TopicReservedError); |
| 58 |
await expect(throwAppError(fakeResponse(429, { code: AccountActionLimitReachedError.CODE }))).rejects.toBeInstanceOf( |
| 59 |
AccountActionLimitReachedError, |
| 60 |
); |
| 61 |
}); |
| 62 |
|
| 63 |
it("wraps an unknown code that carries an error message in a generic Error", async () => { |
| 64 |
await expect(throwAppError(fakeResponse(400, { code: 12345, error: "boom" }))).rejects.toThrow("Error 12345: boom"); |
| 65 |
}); |
| 66 |
|
| 67 |
it("throws a generic 'Unexpected response' error when there is no ntfy error body", async () => { |
| 68 |
await expect(throwAppError(fakeResponse(500))).rejects.toThrow("Unexpected response 500"); |
| 69 |
}); |
| 70 |
}); |
| 71 |
|
| 72 |
describe("fetchOrThrow", () => { |
| 73 |
afterEach(() => { |
| 74 |
vi.unstubAllGlobals(); |
| 75 |
}); |
| 76 |
|
| 77 |
it("returns the response unchanged on HTTP 200", async () => { |
| 78 |
const response = { status: 200 }; |
| 79 |
vi.stubGlobal( |
| 80 |
"fetch", |
| 81 |
vi.fn(async () => response), |
| 82 |
); |
| 83 |
await expect(fetchOrThrow("https://ntfy.sh/mytopic/json")).resolves.toBe(response); |
| 84 |
}); |
| 85 |
|
| 86 |
it("throws on a non-200 response", async () => { |
| 87 |
vi.stubGlobal( |
| 88 |
"fetch", |
| 89 |
vi.fn(async () => fakeResponse(401)), |
| 90 |
); |
| 91 |
await expect(fetchOrThrow("https://ntfy.sh/mytopic/json")).rejects.toBeInstanceOf(UnauthorizedError); |
| 92 |
}); |
| 93 |
}); |