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("loads and refreshes when a transport rejects subscriptions synchronously", async () => { |
| 24 | const { coline } = setup(); |
| 25 | const records = coline.storage.collection<{ title: string }>("issues"); |
| 26 | const row = await records.insert({ title: "Before" }); |
| 27 | const client = { |
| 28 | ...coline, |
| 29 | realtime: { |
| 30 | ...coline.realtime, |
| 31 | subscribe: () => { |
| 32 | throw new Error("Subscriptions unavailable"); |
| 33 | }, |
| 34 | }, |
| 35 | }; |
| 36 | const live = createLiveCollection<{ title: string }>(client, "issues"); |
| 37 | try { |
| 38 | await live.start(); |
| 39 | expect(live.getSnapshot().records[0]?.data.title).toBe("Before"); |
| 40 | live.stop(); |
| 41 | await records.update(row.id, { title: "Recovered" }); |
| 42 | await live.start(); |
| 43 | expect(live.getSnapshot().records[0]?.data.title).toBe("Recovered"); |
| 44 | } finally { |
| 45 | live.dispose(); |
| 46 | } |
| 47 | }); |
| 48 |
| 49 | it("sorts numeric fields consistently before and after optimistic edits", async () => { |
| 50 | const { coline } = setup(); |
| 51 | const rows = coline.storage.collection<{ priority: number }>("ranking"); |
| 52 | const second = await rows.insert({ priority: 2 }); |
| 53 | await rows.insert({ priority: 10 }); |
| 54 | await rows.insert({ priority: -1 }); |
| 55 | const query = { orderBy: "priority", order: "asc" } as const; |
| 56 | expect((await rows.query(query)).records.map((row) => row.data.priority)).toEqual([-1, 2, 10]); |
| 57 | const live = createLiveCollection<{ priority: number }>(coline, "ranking", query); |
| 58 | try { |
| 59 | await live.start(); |
| 60 | expect(live.getSnapshot().records.map((row) => row.data.priority)).toEqual([-1, 2, 10]); |
| 61 | const saving = live.update(second.id, { priority: 20 }); |
| 62 | expect(live.getSnapshot().records.map((row) => row.data.priority)).toEqual([-1, 10, 20]); |
| 63 | await saving; |
| 64 | await live.refresh(); |
| 65 | expect(live.getSnapshot().records.map((row) => row.data.priority)).toEqual([-1, 10, 20]); |
| 66 | expect( |
| 67 | (await rows.query({ ...query, order: "desc" })).records.map((row) => row.data.priority), |
| 68 | ).toEqual([20, 10, -1]); |
| 69 | } finally { |
| 70 | live.dispose(); |
| 71 | } |
| 72 | }); |
| 73 | it("renders a local edit before a delayed network write and reconciles the acknowledgement", async () => { |
| 74 | const { coline } = setup(); |
| 75 | const records = coline.storage.collection<{ title: string }>("issues"); |
| 76 | const row = await records.insert({ title: "Before" }); |
| 77 | let release!: () => void; |
| 78 | const delayed = { |
| 79 | ...coline, |
| 80 | storage: { |
| 81 | ...coline.storage, |
| 82 | collection: () => ({ |
| 83 | ...records, |
| 84 | update: async (...args: Parameters<typeof records.update>) => { |
| 85 | await new Promise<void>((resolve) => { |
| 86 | release = resolve; |
| 87 | }); |
| 88 | return records.update(...args); |
| 89 | }, |
| 90 | }), |
| 91 | }, |
| 92 | } as ColineCapabilities; |
| 93 | const live = createLiveCollection<{ title: string }>(delayed, "issues"); |
| 94 | await live.start(); |
| 95 | const start = performance.now(); |
| 96 | const saved = live.update(row.id, { title: "After" }); |
| 97 | expect(live.getSnapshot().records[0]?.data.title).toBe("After"); |
| 98 | expect(performance.now() - start).toBeLessThan(50); |
| 99 | expect(live.getSnapshot().pending).toBe(1); |
| 100 | expect((await records.get(row.id))?.data.title).toBe("Before"); |
| 101 | release(); |
| 102 | await saved; |
| 103 | expect(live.getSnapshot().pending).toBe(0); |
| 104 | live.dispose(); |
| 105 | }); |
| 106 | it("synchronizes two clients and recovers changes missed while stopped", async () => { |
| 107 | const { coline, state } = setup(); |
| 108 | const other = setup("member", state).coline; |
| 109 | const a = createLiveCollection<{ title: string }>(coline, "issues"), |
| 110 | b = createLiveCollection<{ title: string }>(other, "issues"); |
| 111 | await Promise.all([a.start(), b.start()]); |
| 112 | const row = await a.insert({ title: "Shared" }); |
| 113 | await tick(); |
| 114 | await b.refresh(); |
| 115 | expect(b.getSnapshot().records[0]?.id).toBe(row?.id); |
| 116 | b.stop(); |
| 117 | await a.update(row!.id, { title: "While disconnected" }); |
| 118 | await b.start(); |
| 119 | expect(b.getSnapshot().records[0]?.data.title).toBe("While disconnected"); |
| 120 | a.dispose(); |
| 121 | b.dispose(); |
| 122 | }); |
| 123 | it("rejects stale edits and rolls back instead of silently overwriting another person", async () => { |
| 124 | const { coline } = setup(); |
| 125 | const records = coline.storage.collection<{ title: string }>("issues"); |
| 126 | const row = await records.insert({ title: "Original" }); |
| 127 | const noHints = { |
| 128 | ...coline, |
| 129 | realtime: { ...coline.realtime, subscribe: async () => ({ unsubscribe() {} }) }, |
| 130 | }; |
| 131 | const live = createLiveCollection<{ title: string }>(noHints, "issues"); |
| 132 | await live.start(); |
| 133 | await records.update(row.id, { title: "Their edit" }, { expectedVersion: 1 }); |
| 134 | await expect(live.update(row.id, { title: "My stale edit" })).rejects.toMatchObject({ |
| 135 | code: "record_conflict", |
| 136 | }); |
| 137 | await tick(); |
| 138 | expect(live.getSnapshot().records[0]?.data.title).toBe("Their edit"); |
| 139 | expect(live.getSnapshot().pending).toBe(0); |
| 140 | live.dispose(); |
| 141 | }); |
| 142 | it("serializes rapid local edits without losing either acknowledgement", async () => { |
| 143 | const { coline } = setup(); |
| 144 | const live = createLiveCollection<{ title: string; priority: number }>(coline, "issues"); |
| 145 | await live.start(); |
| 146 | const row = await live.insert({ title: "Initial", priority: 0 }); |
| 147 | await Promise.all([ |
| 148 | live.update(row!.id, { title: "Updated" }), |
| 149 | live.update(row!.id, { priority: 1 }), |
| 150 | ]); |
| 151 | expect(live.getSnapshot().records[0]?.data).toEqual({ title: "Updated", priority: 1 }); |
| 152 | expect(live.getSnapshot().records[0]?.version).toBe(3); |
| 153 | live.dispose(); |
| 154 | }); |
| 155 | it("filters private records before pagination and evicts a record after access is revoked", async () => { |
| 156 | const { coline, state } = setup(); |
| 157 | const other = setup("member", state).coline; |
| 158 | const records = coline.storage.collection<{ title: string }>("issues"); |
| 159 | const privateRecord = await records.insert( |
| 160 | { title: "Hidden" }, |
| 161 | { access: { workspace: "none", readers: [], writers: [] } }, |
| 162 | ); |
| 163 | await expect( |
| 164 | other.references.resolve({ kind: "app_record", id: privateRecord.id }), |
| 165 | ).rejects.toThrow(); |
| 166 | const row = await records.insert( |
| 167 | { title: "Shared" }, |
| 168 | { access: { workspace: "none", readers: ["member"], writers: [] } }, |
| 169 | ); |
| 170 | const live = createLiveCollection<{ title: string }>(other, "issues", { limit: 1 }); |
| 171 | await live.start(); |
| 172 | expect(live.getSnapshot().records.map((row) => row.data.title)).toEqual(["Shared"]); |
| 173 | await expect( |
| 174 | other.storage.collection("issues").update(row.id, { title: "Denied" }), |
| 175 | ).rejects.toMatchObject({ code: "record_forbidden" }); |
| 176 | await records.setAccess( |
| 177 | row.id, |
| 178 | { workspace: "none", readers: [], writers: [] }, |
| 179 | { expectedVersion: 1 }, |
| 180 | ); |
| 181 | await live.refresh(); |
| 182 | expect(live.getSnapshot().records).toEqual([]); |
| 183 | live.dispose(); |
| 184 | }); |
| 185 | it.each([ |
| 186 | "actor_not_member", |
| 187 | "APP_CAPABILITY_ACTOR_NOT_MEMBER", |
| 188 | "APP_CAPABILITY_PERMISSION_NOT_GRANTED", |
| 189 | "APP_CAPABILITY_INSTALL_NOT_ACTIVE", |
| 190 | "APP_CAPABILITY_ENTITLEMENT_REQUIRED", |
| 191 | "APP_INSTALL_NOT_FOUND", |
| 192 | "APP_INVOCATION_TOKEN_EXPIRED", |
| 193 | ])("does not resurrect cached data after authorization denial %s", async (code) => { |
| 194 | const { coline } = setup(); |
| 195 | const records = coline.storage.collection<{ title: string }>("issues"); |
| 196 | const row = await records.insert({ title: "Private" }); |
| 197 | let denied = false; |
| 198 | let release!: () => void; |
| 199 | const transport = { |
| 200 | ...coline, |
| 201 | storage: { |
| 202 | ...coline.storage, |
| 203 | collection: () => ({ |
| 204 | ...records, |
| 205 | sync: async (...args: Parameters<typeof records.sync>) => { |
| 206 | if (denied) throw Object.assign(new Error("Access revoked"), { code }); |
| 207 | return records.sync(...args); |
| 208 | }, |
| 209 | update: async (...args: Parameters<typeof records.update>) => { |
| 210 | const result = await records.update(...args); |
| 211 | await new Promise<void>((resolve) => { |
| 212 | release = resolve; |
| 213 | }); |
| 214 | return result; |
| 215 | }, |
| 216 | }), |
| 217 | }, |
| 218 | } as ColineCapabilities; |
| 219 | const live = createLiveCollection<{ title: string }>(transport, "issues"); |
| 220 | await live.start(); |
| 221 | const rejected = live.update(row.id, { title: "Sent" }).catch((error) => error); |
| 222 | await tick(); |
| 223 | denied = true; |
| 224 | await live.refresh(); |
| 225 | expect((await rejected).code).toBe(code); |
| 226 | expect(live.getSnapshot().records).toEqual([]); |
| 227 | release(); |
| 228 | await tick(); |
| 229 | expect(live.getSnapshot().records).toEqual([]); |
| 230 | live.dispose(); |
| 231 | }); |
| 232 |
| 233 | it("stops timers and subscriptions, including a late subscription acknowledgement", async () => { |
| 234 | vi.useFakeTimers(); |
| 235 | const { coline } = setup(); |
| 236 | let complete!: (value: { unsubscribe: () => void }) => void; |
| 237 | const unsubscribe = vi.fn(); |
| 238 | coline.realtime.subscribe = () => |
| 239 | new Promise((resolve) => { |
| 240 | complete = resolve; |
| 241 | }); |
| 242 | const live = createLiveCollection(coline, "issues"); |
| 243 | await live.start(); |
| 244 | live.dispose(); |
| 245 | complete({ unsubscribe }); |
| 246 | await Promise.resolve(); |
| 247 | expect(unsubscribe).toHaveBeenCalledOnce(); |
| 248 | expect(vi.getTimerCount()).toBe(0); |
| 249 | vi.useRealTimers(); |
| 250 | }); |
| 251 | }); |
| 252 |