Open History to see the last commit
1import { describe, expect, it } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import * as Option from "effect/Option";
4import { vi } from "vite-plus/test";
5
6const openDatabaseAsync = vi.hoisted(() => vi.fn());
7
8vi.mock("expo-sqlite", () => ({ openDatabaseAsync }));
9
10import * as MobileDatabase from "./mobile-database";
11
12describe("mobile database legacy cache migration", () => {
13 it("maps legacy thread records to their SQLite identity", () => {
14 const payload = JSON.stringify({
15 schemaVersion: 2,
16 environmentId: "environment-1",
17 threadId: "thread-1",
18 snapshot: {},
19 });
20
21 expect(MobileDatabase.decodeLegacyCacheRecord("connection-thread-snapshots", payload)).toEqual({
22 environmentId: "environment-1",
23 kind: "thread",
24 cacheKey: "thread-1",
25 schemaVersion: 2,
26 payload,
27 });
28 });
29
30 it("skips malformed legacy records", () => {
31 expect(MobileDatabase.decodeLegacyCacheRecord("connection-vcs-refs", "{not-json")).toBeNull();
32 expect(
33 MobileDatabase.decodeLegacyCacheRecord(
34 "connection-vcs-refs",
35 JSON.stringify({ schemaVersion: 1, environmentId: "environment-1" }),
36 ),
37 ).toBeNull();
38 });
39});
40
41describe("offline pull request cache", () => {
42 const environmentId = "cached-environment" as import("@t3tools/contracts").EnvironmentId;
43 const otherEnvironment = "other-environment" as import("@t3tools/contracts").EnvironmentId;
44
45 async function sqliteDatabase() {
46 const { DatabaseSync } = await import("node:sqlite");
47 const sqlite = new DatabaseSync(":memory:");
48 sqlite.exec("PRAGMA user_version = 1");
49 const database = {
50 execAsync: async (sql: string) => {
51 sqlite.exec(sql);
52 },
53 runAsync: async (sql: string, ...args: Array<string | number>) =>
54 sqlite.prepare(sql).run(...args),
55 getFirstAsync: async (sql: string, ...args: Array<string | number>) =>
56 sqlite.prepare(sql).get(...args),
57 getAllAsync: async (sql: string, ...args: Array<string | number>) =>
58 sqlite.prepare(sql).all(...args),
59 withExclusiveTransactionAsync: async (run: (transaction: unknown) => Promise<void>) => {
60 await run(database);
61 },
62 closeAsync: async () => {
63 sqlite.close();
64 },
65 };
66 openDatabaseAsync.mockResolvedValueOnce(database);
67 return sqlite;
68 }
69
70 it.effect("restores saved records and clears them with the environment", () =>
71 Effect.scoped(
72 Effect.gen(function* () {
73 yield* Effect.promise(sqliteDatabase);
74 const database = yield* MobileDatabase.make;
75 yield* database.saveCache(environmentId, "pull-request", "v1:list", 1, '{"entries":[]}');
76 yield* database.saveCache(otherEnvironment, "pull-request", "v1:list", 1, "other");
77 expect(yield* database.loadCache(environmentId, "pull-request", "v1:list")).toEqual(
78 Option.some('{"entries":[]}'),
79 );
80 yield* database.clearEnvironmentCache(environmentId);
81 expect((yield* database.loadCache(environmentId, "pull-request", "v1:list"))._tag).toBe(
82 "None",
83 );
84 expect((yield* database.loadCache(otherEnvironment, "pull-request", "v1:list"))._tag).toBe(
85 "Some",
86 );
87 }),
88 ),
89 );
90});