Open History to see the last commit
1import { describe, expect, it } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import { vi } from "vite-plus/test";
4
5const openDatabaseAsync = vi.hoisted(() => vi.fn());
6
7vi.mock("expo-sqlite", () => ({ openDatabaseAsync }));
8
9import * as MobileDatabase from "./mobile-database";
10
11describe("mobile database legacy cache migration", () => {
12 it.effect("keeps acquisition failures typed on database operations", () =>
13 Effect.scoped(
14 Effect.gen(function* () {
15 openDatabaseAsync.mockRejectedValueOnce(new Error("SQLite unavailable"));
16
17 const database = yield* MobileDatabase.make;
18 const result = yield* Effect.result(database.loadPreferencesJson);
19
20 expect(result).toMatchObject({
21 _tag: "Failure",
22 failure: { _tag: "MobileDatabaseError", operation: "open" },
23 });
24 }),
25 ),
26 );
27
28 it("maps legacy thread records to their SQLite identity", () => {
29 const payload = JSON.stringify({
30 schemaVersion: 2,
31 environmentId: "environment-1",
32 threadId: "thread-1",
33 snapshot: {},
34 });
35
36 expect(MobileDatabase.decodeLegacyCacheRecord("connection-thread-snapshots", payload)).toEqual({
37 environmentId: "environment-1",
38 kind: "thread",
39 cacheKey: "thread-1",
40 schemaVersion: 2,
41 payload,
42 });
43 });
44
45 it("preserves the old shell payload for schema decoding after migration", () => {
46 const payload = JSON.stringify({
47 schemaVersion: 1,
48 environmentId: "environment-1",
49 snapshotReceivedAt: "2026-07-01T00:00:00.000Z",
50 snapshot: {},
51 });
52
53 expect(MobileDatabase.decodeLegacyCacheRecord("shell-snapshots", payload)).toEqual({
54 environmentId: "environment-1",
55 kind: "shell",
56 cacheKey: "snapshot",
57 schemaVersion: 1,
58 payload,
59 });
60 });
61
62 it("skips malformed legacy records", () => {
63 expect(MobileDatabase.decodeLegacyCacheRecord("connection-vcs-refs", "{not-json")).toBeNull();
64 expect(
65 MobileDatabase.decodeLegacyCacheRecord(
66 "connection-vcs-refs",
67 JSON.stringify({ schemaVersion: 1, environmentId: "environment-1" }),
68 ),
69 ).toBeNull();
70 });
71});