Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

environment-cache-store.test.ts

97 lines3.5 KB

1import { EnvironmentId, type VcsListRefsResult } from "@t3tools/contracts";
2import { describe, expect, it } from "@effect/vitest";
3import * as Effect from "effect/Effect";
4import * as Option from "effect/Option";
5​
6import { type ClientCacheKind, MobileDatabase } from "../persistence/mobile-database";
7import { make } from "./environment-cache-store";
8​
9const ENVIRONMENT_ID = EnvironmentId.make("environment-1");
10const REFS: VcsListRefsResult = {
11 refs: [
12 {
13 name: "main",
14 current: true,
15 isDefault: true,
16 worktreePath: "/repo",
17 },
18 ],
19 isRepo: true,
20 hasPrimaryRemote: true,
21 nextCursor: null,
22 totalCount: 1,
23};
24​
25function cacheId(environmentId: EnvironmentId, kind: ClientCacheKind, cacheKey: string) {
26 return `${environmentId}:${kind}:${cacheKey}`;
28​
29function makeDatabase() {
30 const values = new Map<string, string>();
31 const removed: Array<string> = [];
32 const database = MobileDatabase.of({
33 loadCache: (environmentId, kind, cacheKey) =>
34 Effect.succeed(Option.fromUndefinedOr(values.get(cacheId(environmentId, kind, cacheKey)))),
35 saveCache: (environmentId, kind, cacheKey, _schemaVersion, payload) =>
36 Effect.sync(() => {
37 values.set(cacheId(environmentId, kind, cacheKey), payload);
38 }),
39 removeCache: (environmentId, kind, cacheKey) =>
40 Effect.sync(() => {
41 const id = cacheId(environmentId, kind, cacheKey);
42 removed.push(id);
43 values.delete(id);
44 }),
45 clearEnvironmentCache: (environmentId) =>
46 Effect.sync(() => {
47 for (const key of values.keys()) {
48 if (key.startsWith(`${environmentId}:`)) values.delete(key);
49 }
50 }),
51 clearAllCaches: Effect.sync(() => values.clear()),
52 inspectCaches: Effect.succeed([]),
53 loadPreferencesJson: Effect.succeed(Option.none()),
54 savePreferencesJson: () => Effect.void,
55 });
56 return { database, removed, values };
58​
59describe("mobile SQLite environment cache store", () => {
60 it.effect("round-trips schema-validated VCS refs", () =>
61 Effect.gen(function* () {
62 const memory = makeDatabase();
63 const store = yield* make().pipe(Effect.provideService(MobileDatabase, memory.database));
64​
65 yield* store.saveVcsRefs(ENVIRONMENT_ID, "/repo", REFS);
66​
67 expect(yield* store.loadVcsRefs(ENVIRONMENT_ID, "/repo")).toEqual(Option.some(REFS));
68 }),
69 );
70​
71 it.effect("deletes a corrupt cache record and treats it as a miss", () =>
72 Effect.gen(function* () {
73 const memory = makeDatabase();
74 const store = yield* make().pipe(Effect.provideService(MobileDatabase, memory.database));
75 const id = cacheId(ENVIRONMENT_ID, "vcs-refs", "/repo");
76 memory.values.set(id, "{not-json");
77​
78 expect(yield* store.loadVcsRefs(ENVIRONMENT_ID, "/repo")).toEqual(Option.none());
79 expect(memory.removed).toEqual([id]);
80 }),
81 );
82​
83 it.effect("clears one environment without touching another", () =>
84 Effect.gen(function* () {
85 const memory = makeDatabase();
86 const store = yield* make().pipe(Effect.provideService(MobileDatabase, memory.database));
87 const otherEnvironmentId = EnvironmentId.make("environment-2");
88 yield* store.saveVcsRefs(ENVIRONMENT_ID, "/repo", REFS);
89 yield* store.saveVcsRefs(otherEnvironmentId, "/repo", REFS);
90​
91 yield* store.clear(ENVIRONMENT_ID);
92​
93 expect(yield* store.loadVcsRefs(ENVIRONMENT_ID, "/repo")).toEqual(Option.none());
94 expect(yield* store.loadVcsRefs(otherEnvironmentId, "/repo")).toEqual(Option.some(REFS));
95 }),
96 );
97});