test: verify share bot API contract

This commit is contained in:
2026-08-16 01:19:20 +07:00 Unverified
parent c9f01231c6
commit f32171a576
3 changed files with 161 additions and 56 deletions
+60
View File
@@ -0,0 +1,60 @@
import { describe, expect, test } from "bun:test";
import {
buildBotShareResponse,
hasValidShareBotBearer,
rejectSuppliedBotProfile,
resolveBotSenderProfile,
} from "@/lib/share/bot";
describe("share bot contract", () => {
test("requires an exact bearer secret", () => {
expect(hasValidShareBotBearer(null, "secret")).toBeFalse();
expect(hasValidShareBotBearer("Basic secret", "secret")).toBeFalse();
expect(hasValidShareBotBearer("Bearer wrong", "secret")).toBeFalse();
expect(hasValidShareBotBearer("Bearer secret", "secret")).toBeTrue();
});
test("rejects request-provided identity snapshots", () => {
const formData = new FormData();
formData.set("displayName", "Untrusted name");
expect(() => rejectSuppliedBotProfile(formData)).toThrow("must not be supplied");
});
test("distinguishes an invalid guild sender from lookup downtime", async () => {
await expect(
resolveBotSenderProfile("1", async () => null),
).rejects.toMatchObject({ status: 422 });
await expect(
resolveBotSenderProfile("1", async () => {
throw new Error("Discord unavailable");
}),
).rejects.toMatchObject({ status: 503 });
});
test("returns the documented successful response shape", () => {
expect(
buildBotShareResponse({
baseUrl: "https://example.com",
share: {
id: "a1b2c3d4",
createdAt: new Date("2026-08-16T00:00:00.000Z"),
},
displayName: "Example User",
avatarUrl: "https://cdn.discordapp.com/avatar.png",
imagePath: "/api/cdn/image-1",
}),
).toEqual({
success: true,
share: {
id: "a1b2c3d4",
url: "https://example.com/share/a1b2c3d4",
author: {
displayName: "Example User",
avatarUrl: "https://cdn.discordapp.com/avatar.png",
},
imageUrl: "https://example.com/api/cdn/image-1",
createdAt: "2026-08-16T00:00:00.000Z",
},
});
});
});