Files
webmail/lib/store/__tests__/helpers/node-adapter.ts
T
Linus Rath 687e7cc012 feat: move offline mode onto the encrypted local store
The IndexedDB cache, its crypto, the responder and the page-side sync
go. lib/offline keeps its structure, policy, wipes, sign-out gates and
notices; answers, the outbox and sync now live in lib/store, in a worker
behind lib/store/client/api.ts.

What users get besides that:
- every folder is kept, not just the Inbox and opened ones
- delete for good and empty trash work offline
- a change the server refuses, or that meets a message changed on
  another device, is shown instead of dropped
- a send whose response got lost is looked up by a Message-ID fixed when
  it was queued, then finished, submitted or sent again
- with the store on, a start paints from the local copy, then connects

An account the app drops by itself no longer wipes its data: only a
sign-out or an explicit removal does. The outbox of the old database
moves over once, in lib/offline/migrate-legacy.ts, and the old database
goes only after the store committed. `isNetworkFailure` now counts
timeouts and proxy 5xx. Sends and outbox requests are never answered
from the device.

Tests moved with the code: cache.test.ts and responder.test.ts are
replaced by lib/store/__tests__/, and review-fixes.test.ts, outbox.test.ts
and client-fallback.test.ts now run the real engine on node:sqlite.
Changed assertions: an uncertain send is looked up rather than left alone
forever, destroy and empty trash are queued rather than refused, and #9
(opened folders) became "a folder the server deleted goes", since all
folders are kept.
2026-11-03 05:54:39 +01:00

40 lines
1.6 KiB
TypeScript

/** The storage adapter for tests: real SQLite with FTS5 through node:sqlite. */
import type { DatabaseSync, SQLInputValue } from 'node:sqlite';
import { runTransaction, type SqlRow, type SqlValue, type StorageAdapter } from '../../storage';
// Not an import: Vite refuses node:sqlite in the jsdom tests of lib/offline.
const sqlite = process.getBuiltinModule('node:sqlite');
export function openMemoryStore(): StorageAdapter {
return adapterFor(new sqlite.DatabaseSync(':memory:'));
}
export function openFileStore(file: string): StorageAdapter {
return adapterFor(new sqlite.DatabaseSync(file));
}
function adapterFor(db: DatabaseSync): StorageAdapter {
const tx = { depth: 0 };
const prepared = new Map<string, ReturnType<DatabaseSync['prepare']>>();
const statement = (sql: string) => {
let stmt = prepared.get(sql);
if (!stmt) {
stmt = db.prepare(sql);
prepared.set(sql, stmt);
}
return stmt;
};
return {
exec: (sql) => db.exec(sql),
run: (sql, params = []) => {
const result = statement(sql).run(...(params as SQLInputValue[]));
return { changes: Number(result.changes), lastInsertRowid: Number(result.lastInsertRowid) };
},
all: <T extends object = SqlRow>(sql: string, params: readonly SqlValue[] = []) => statement(sql).all(...(params as SQLInputValue[])) as T[],
get: <T extends object = SqlRow>(sql: string, params: readonly SqlValue[] = []) => statement(sql).get(...(params as SQLInputValue[])) as T | undefined,
transaction: (fn) => runTransaction(tx, (sql) => db.exec(sql), fn),
close: () => db.close(),
};
}