55 lines
2.3 KiB
TypeScript
55 lines
2.3 KiB
TypeScript
import { describe, expect, test } from "bun:test";
|
|
import { getShareContentForClipboard } from "@/lib/share/copy";
|
|
import { requireShareSessionIdentity } from "@/lib/share/session";
|
|
|
|
describe("share UI contracts", () => {
|
|
test("copies exact share content without reading the description", () => {
|
|
const requestedIds: string[] = [];
|
|
const exactContent = " first line\r\n\tsecond line \n";
|
|
const content = getShareContentForClipboard((id) => {
|
|
requestedIds.push(id);
|
|
if (id === "share-content") return { textContent: exactContent };
|
|
if (id === "share-description") return { textContent: "description" };
|
|
return null;
|
|
});
|
|
|
|
expect(content).toBe(exactContent);
|
|
expect(requestedIds).toEqual(["share-content"]);
|
|
});
|
|
|
|
test("rejects an unauthenticated web upload identity with 401", () => {
|
|
expect(() => requireShareSessionIdentity(null)).toThrow("Unauthorized");
|
|
try {
|
|
requireShareSessionIdentity({ user: { id: "user-1" } });
|
|
throw new Error("Expected a missing Discord ID to fail");
|
|
} catch (error) {
|
|
expect(error).toMatchObject({ status: 401 });
|
|
}
|
|
expect(
|
|
requireShareSessionIdentity({
|
|
user: { id: "user-1", discordId: "123456789012345678" },
|
|
}),
|
|
).toEqual({ userId: "user-1", discordId: "123456789012345678" });
|
|
});
|
|
|
|
test("keeps the extracted login dialog non-dismissible and shared", async () => {
|
|
const [dialog, sharePage, formPage] = await Promise.all([
|
|
Bun.file(new URL("../../components/login-dialog.tsx", import.meta.url)).text(),
|
|
Bun.file(new URL("../../app/share/page.tsx", import.meta.url)).text(),
|
|
Bun.file(new URL("../../app/form/[id]/page.tsx", import.meta.url)).text(),
|
|
]);
|
|
|
|
expect(dialog).toContain("เข้าสู่ระบบก่อนกรอกแบบฟอร์ม");
|
|
expect(dialog).toContain("showCloseButton={false}");
|
|
expect(dialog).toContain("onEscapeKeyDown");
|
|
expect(dialog).toContain("onInteractOutside");
|
|
expect(dialog).toContain("callbackUrl: window.location.href");
|
|
expect(sharePage).toContain('from "@/components/login-dialog"');
|
|
expect(sharePage).toContain("if (!discordId)");
|
|
expect(sharePage.indexOf("if (!discordId)")).toBeLessThan(
|
|
sharePage.indexOf("<ShareUploadClient />"),
|
|
);
|
|
expect(formPage).toContain('from "@/components/login-dialog"');
|
|
});
|
|
});
|