The Coline App SDK: apps, tools, file types and surfaces that run inside Coline workspaces, and public websites with Coline Sites. TypeScript, with an in-memory test workspace. MIT.
| 4 | import type { ColineCapabilities } from "./types"; |
| 5 |
| 6 | function setup(userId = "owner", state = new TestWorkspaceState()) { |
| 7 | const coline = createFakeCapabilities(state, { |
| 8 | workspaceId: "ws", |
| 9 | workspaceSlug: "ws", |
| 10 | workspaceName: "WS", |
| 11 | appKey: "test", |
| 12 | installId: "install", |
| 13 | actor: { kind: "user", userId }, |
| 14 | members: [], |
| 15 | pickers: {}, |
| 16 | onNetFetch: undefined, |
| 17 | onAiGenerate: undefined, |
| 18 | }); |
| 19 | return { state, coline }; |
| 20 | } |
| 21 | const tick = () => new Promise((resolve) => setTimeout(resolve, 0)); |
| 22 | describe("live collections", () => { |
| 23 | it("renders a local edit before a delayed network write and reconciles the acknowledgement", async () => { |
| 24 | const { coline } = setup(); |
| 25 | const records = coline.storage.collection<{ title: string }>("issues"); |
| 26 | const row = await records.insert({ title: "Before" }); |
| 27 | let release!: () => void; |
| 28 | const delayed = { |
| 29 | ...coline, |
| 30 | storage: { |
| 31 | ...coline.storage, |
| 32 | collection: () => ({ |
| 33 | ...records, |
| 34 | update: async (...args: Parameters<typeof records.update>) => { |
| 35 | await new Promise<void>((resolve) => { |
| 36 | release = resolve; |
| 37 | }); |
| 38 | return records.update(...args); |
| 39 | }, |
| 40 | }), |
| 41 | }, |
| 42 | } as ColineCapabilities; |
| 43 | const live = createLiveCollection<{ title: string }>(delayed, "issues"); |
| 44 | await live.start(); |
| 45 | const start = performance.now(); |
| 46 | const saved = live.update(row.id, { title: "After" }); |
| 47 | expect(live.getSnapshot().records[0]?.data.title).toBe("After"); |
| 48 | expect(performance.now() - start).toBeLessThan(50); |
| 49 | expect(live.getSnapshot().pending).toBe(1); |
| 50 | expect((await records.get(row.id))?.data.title).toBe("Before"); |
| 51 | release(); |
| 52 | await saved; |
| 53 | expect(live.getSnapshot().pending).toBe(0); |
| 54 | live.dispose(); |
| 55 | }); |
| 56 | it("synchronizes two clients and recovers changes missed while stopped", async () => { |
| 57 | const { coline, state } = setup(); |
| 58 | const other = setup("member", state).coline; |
| 59 | const a = createLiveCollection<{ title: string }>(coline, "issues"), |
| 60 | b = createLiveCollection<{ title: string }>(other, "issues"); |
| 61 | await Promise.all([a.start(), b.start()]); |
| 62 | const row = await a.insert({ title: "Shared" }); |
| 63 | await tick(); |
| 64 | await b.refresh(); |
| 65 | expect(b.getSnapshot().records[0]?.id).toBe(row?.id); |
| 66 | b.stop(); |
| 67 | await a.update(row!.id, { title: "While disconnected" }); |
| 68 | await b.start(); |
| 69 | expect(b.getSnapshot().records[0]?.data.title).toBe("While disconnected"); |
| 70 | a.dispose(); |
| 71 | b.dispose(); |
| 72 | }); |
| 73 | it("rejects stale edits and rolls back instead of silently overwriting another person", async () => { |
| 74 | const { coline } = setup(); |
| 75 | const records = coline.storage.collection<{ title: string }>("issues"); |
| 76 | const row = await records.insert({ title: "Original" }); |
| 77 | const noHints = { |
| 78 | ...coline, |
| 79 | realtime: { ...coline.realtime, subscribe: async () => ({ unsubscribe() {} }) }, |
| 80 | }; |
| 81 | const live = createLiveCollection<{ title: string }>(noHints, "issues"); |
| 82 | await live.start(); |
| 83 | await records.update(row.id, { title: "Their edit" }, { expectedVersion: 1 }); |
| 84 | await expect(live.update(row.id, { title: "My stale edit" })).rejects.toMatchObject({ |
| 85 | code: "record_conflict", |
| 86 | }); |
| 87 | await tick(); |
| 88 | expect(live.getSnapshot().records[0]?.data.title).toBe("Their edit"); |
| 89 | expect(live.getSnapshot().pending).toBe(0); |
| 90 | live.dispose(); |
| 91 | }); |
| 92 | it("serializes rapid local edits without losing either acknowledgement", async () => { |
| 93 | const { coline } = setup(); |
| 94 | const live = createLiveCollection<{ title: string; priority: number }>(coline, "issues"); |
| 95 | await live.start(); |
| 96 | const row = await live.insert({ title: "Initial", priority: 0 }); |
| 97 | await Promise.all([ |
| 98 | live.update(row!.id, { title: "Updated" }), |
| 99 | live.update(row!.id, { priority: 1 }), |
| 100 | ]); |
| 101 | expect(live.getSnapshot().records[0]?.data).toEqual({ title: "Updated", priority: 1 }); |
| 102 | expect(live.getSnapshot().records[0]?.version).toBe(3); |
| 103 | live.dispose(); |
| 104 | }); |
| 105 | it("filters private records before pagination and evicts a record after access is revoked", async () => { |
| 106 | const { coline, state } = setup(); |
| 107 | const other = setup("member", state).coline; |
| 108 | const records = coline.storage.collection<{ title: string }>("issues"); |
| 109 | const privateRecord = await records.insert( |
| 110 | { title: "Hidden" }, |
| 111 | { access: { workspace: "none", readers: [], writers: [] } }, |
| 112 | ); |
| 113 | await expect( |
| 114 | other.references.resolve({ kind: "app_record", id: privateRecord.id }), |
| 115 | ).rejects.toThrow(); |
| 116 | const row = await records.insert( |
| 117 | { title: "Shared" }, |
| 118 | { access: { workspace: "none", readers: ["member"], writers: [] } }, |
| 119 | ); |
| 120 | const live = createLiveCollection<{ title: string }>(other, "issues", { limit: 1 }); |
| 121 | await live.start(); |
| 122 | expect(live.getSnapshot().records.map((row) => row.data.title)).toEqual(["Shared"]); |
| 123 | await expect( |
| 124 | other.storage.collection("issues").update(row.id, { title: "Denied" }), |
| 125 | ).rejects.toMatchObject({ code: "record_forbidden" }); |
| 126 | await records.setAccess( |
| 127 | row.id, |
| 128 | { workspace: "none", readers: [], writers: [] }, |
| 129 | { expectedVersion: 1 }, |
| 130 | ); |
| 131 | await live.refresh(); |
| 132 | expect(live.getSnapshot().records).toEqual([]); |
| 133 | live.dispose(); |
| 134 | }); |
| 135 | it("does not resurrect cached data when an old acknowledgement arrives after membership denial", async () => { |
| 136 | const { coline } = setup(); |
| 137 | const records = coline.storage.collection<{ title: string }>("issues"); |
| 138 | const row = await records.insert({ title: "Private" }); |
| 139 | let denied = false; |
| 140 | let release!: () => void; |
| 141 | const transport = { |
| 142 | ...coline, |
| 143 | storage: { |
| 144 | ...coline.storage, |
| 145 | collection: () => ({ |
| 146 | ...records, |
| 147 | sync: async (...args: Parameters<typeof records.sync>) => { |
| 148 | if (denied) |
| 149 | throw Object.assign(new Error("Membership revoked"), { code: "actor_not_member" }); |
| 150 | return records.sync(...args); |
| 151 | }, |
| 152 | update: async (...args: Parameters<typeof records.update>) => { |
| 153 | const result = await records.update(...args); |
| 154 | await new Promise<void>((resolve) => { |
| 155 | release = resolve; |
| 156 | }); |
| 157 | return result; |
| 158 | }, |
| 159 | }), |
| 160 | }, |
| 161 | } as ColineCapabilities; |
| 162 | const live = createLiveCollection<{ title: string }>(transport, "issues"); |
| 163 | await live.start(); |
| 164 | const rejected = live.update(row.id, { title: "Sent" }).catch((error) => error); |
| 165 | await tick(); |
| 166 | denied = true; |
| 167 | await live.refresh(); |
| 168 | expect((await rejected).code).toBe("actor_not_member"); |
| 169 | expect(live.getSnapshot().records).toEqual([]); |
| 170 | release(); |
| 171 | await tick(); |
| 172 | expect(live.getSnapshot().records).toEqual([]); |
| 173 | live.dispose(); |
| 174 | }); |
| 175 |
| 176 | it("stops timers and subscriptions, including a late subscription acknowledgement", async () => { |
| 177 | vi.useFakeTimers(); |
| 178 | const { coline } = setup(); |
| 179 | let complete!: (value: { unsubscribe: () => void }) => void; |
| 180 | const unsubscribe = vi.fn(); |
| 181 | coline.realtime.subscribe = () => |
| 182 | new Promise((resolve) => { |
| 183 | complete = resolve; |
| 184 | }); |
| 185 | const live = createLiveCollection(coline, "issues"); |
| 186 | await live.start(); |
| 187 | live.dispose(); |
| 188 | complete({ unsubscribe }); |
| 189 | await Promise.resolve(); |
| 190 | expect(unsubscribe).toHaveBeenCalledOnce(); |
| 191 | expect(vi.getTimerCount()).toBe(0); |
| 192 | vi.useRealTimers(); |
| 193 | }); |
| 194 | }); |
| 195 |