Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

test(web): add public collection policy unit tests

Richie McIlroye1df0681ef5a
diff --git a/apps/web/__tests__/unit/public-collections-policy.test.ts b/apps/web/__tests__/unit/public-collections-policy.test.ts
new file mode 100644
index 000000000..c05dc14a2
--- /dev/null
+++ b/apps/web/__tests__/unit/public-collections-policy.test.ts
@@ -0,0 +1,110 @@
+import { describe, expect, it } from "vitest";
+import {
+ getPublicCollectionHref,
+ parsePublicCollectionPage,
+ resolvePublicCollectionAccess,
+ resolvePublicCollectionCandidate,
+} from "@/lib/public-collections-policy";
+
+describe("public collections policy", () => {
+ it("parses invalid collection pages as the first page", () => {
+ expect(parsePublicCollectionPage(undefined)).toBe(1);
+ expect(parsePublicCollectionPage("0")).toBe(1);
+ expect(parsePublicCollectionPage("-2")).toBe(1);
+ expect(parsePublicCollectionPage("1.5")).toBe(1);
+ expect(parsePublicCollectionPage(["3", "4"])).toBe(3);
+ });
+
+ it("builds canonical collection page hrefs", () => {
+ expect(getPublicCollectionHref("abc123", 1)).toBe("/c/abc123");
+ expect(getPublicCollectionHref("abc123", 2)).toBe("/c/abc123?page=2");
+ });
+
+ it("resolves public folder before public space on id collisions", () => {
+ const folder = {
+ kind: "folder" as const,
+ public: true,
+ organizationTombstoneAt: null,
+ name: "Folder",
+ };
+ const space = {
+ kind: "space" as const,
+ public: true,
+ organizationTombstoneAt: null,
+ name: "Space",
+ };
+
+ expect(resolvePublicCollectionCandidate(folder, space)).toBe(folder);
+ });
+
+ it("falls through private or tombstoned folders to public spaces", () => {
+ const privateFolder = {
+ kind: "folder" as const,
+ public: false,
+ organizationTombstoneAt: null,
+ };
+ const tombstonedFolder = {
+ kind: "folder" as const,
+ public: true,
+ organizationTombstoneAt: new Date("2026-01-01T00:00:00.000Z"),
+ };
+ const space = {
+ kind: "space" as const,
+ public: true,
+ organizationTombstoneAt: null,
+ };
+
+ expect(resolvePublicCollectionCandidate(privateFolder, space)).toBe(space);
+ expect(resolvePublicCollectionCandidate(tombstonedFolder, space)).toBe(
+ space,
+ );
+ });
+
+ it("blocks tombstoned spaces", () => {
+ const space = {
+ kind: "space" as const,
+ public: true,
+ organizationTombstoneAt: new Date("2026-01-01T00:00:00.000Z"),
+ };
+
+ expect(resolvePublicCollectionCandidate(null, space)).toBeNull();
+ });
+
+ it("applies email restrictions before password checks", () => {
+ expect(
+ resolvePublicCollectionAccess({
+ allowedEmailDomain: "company.com",
+ viewerEmail: null,
+ passwordHash: "hash",
+ verifiedPasswordHash: null,
+ }),
+ ).toEqual({ state: "email_restriction_login_required" });
+
+ expect(
+ resolvePublicCollectionAccess({
+ allowedEmailDomain: "company.com",
+ viewerEmail: "[email protected]",
+ passwordHash: "hash",
+ verifiedPasswordHash: null,
+ }),
+ ).toEqual({ state: "email_restriction_denied" });
+ });
+
+ it("requires the collection password when present", () => {
+ expect(
+ resolvePublicCollectionAccess({
+ viewerEmail: "[email protected]",
+ passwordHash: "space-hash",
+ verifiedPasswordHash: null,
+ }),
+ ).toEqual({ state: "password_required" });
+
+ expect(
+ resolvePublicCollectionAccess({
+ viewerEmail: "[email protected]",
+ passwordHash: "space-hash",
+ verifiedPasswordHash: "space-hash",
+ }),
+ ).toEqual({ state: "allowed" });
+ });
+});