Adopt Commonlib 0.2.0 with injected device storage

This commit is contained in:
vorotamoroz committed 2026-10-23 13:56:54 +00:00
1 parent e56098eb57
commit 96ad2737a7
60 files changed
+1499 -541

No files matched your search

+35 -6
View File
@@ -1,3 +1,4 @@
import { MemoryValueStore } from "@vrtmrz/value-store";
import { createServiceContext } from "@vrtmrz/livesync-commonlib/context";
import { DEFAULT_SETTINGS, SETTING_KEY_P2P_DEVICE_NAME } from "@vrtmrz/livesync-commonlib/compat/common/types";
import type { SimpleStore } from "octagonal-wheels/databases/SimpleStoreBase";
@@ -27,6 +28,7 @@ describe("WebPeer runtime composition", () => {
translate: (key) => `webpeer:${key}`,
});
const runtime = new WebPeerRuntime({
deviceValues: new MemoryValueStore(),
context,
store: createMemoryStore(),
});
@@ -43,6 +45,7 @@ describe("WebPeer runtime composition", () => {
it("loads settings and opens the local database before announcing layout readiness", async () => {
const runtime = new WebPeerRuntime({
deviceValues: new MemoryValueStore(),
store: createMemoryStore(),
});
const loadSettings = vi.spyOn(runtime.services.setting, "loadSettings").mockResolvedValue(undefined);
@@ -68,6 +71,7 @@ describe("WebPeer runtime composition", () => {
vi.useFakeTimers();
try {
const runtime = new WebPeerRuntime({
deviceValues: new MemoryValueStore(),
store: createMemoryStore(),
});
vi.spyOn(runtime.services.setting, "loadSettings").mockResolvedValue(undefined);
@@ -77,9 +81,7 @@ describe("WebPeer runtime composition", () => {
P2P_Enabled: true,
});
vi.spyOn(runtime.services.database, "openDatabase").mockResolvedValue(true);
const onResumed = vi
.spyOn(runtime.services.appLifecycle, "onResumed")
.mockResolvedValue(true);
const onResumed = vi.spyOn(runtime.services.appLifecycle, "onResumed").mockResolvedValue(true);
const open = vi.spyOn(runtime.p2p.transportLifecycle, "connect").mockResolvedValue(undefined);
await runtime.start();
@@ -94,9 +96,11 @@ describe("WebPeer runtime composition", () => {
it("rejects start when the browser-local database cannot be opened", async () => {
const runtime = new WebPeerRuntime({
deviceValues: new MemoryValueStore(),
store: createMemoryStore(),
});
vi.spyOn(runtime.services.setting, "loadSettings").mockResolvedValue(undefined);
vi.spyOn(runtime.services.setting, "currentSettings").mockReturnValue({ ...DEFAULT_SETTINGS });
vi.spyOn(runtime.services.database, "openDatabase").mockResolvedValue(false);
await expect(runtime.start()).rejects.toThrow("WebPeer local database could not be opened");
@@ -104,6 +108,7 @@ describe("WebPeer runtime composition", () => {
it("isolates a specialised runtime and applies its device name before opening the database", async () => {
const runtime = new WebPeerRuntime({
deviceValues: new MemoryValueStore(),
store: createMemoryStore(),
deviceName: " p2p-check-browser-desktop-abc ",
systemVaultName: "p2p-check-vault",
@@ -114,15 +119,39 @@ describe("WebPeer runtime composition", () => {
P2P_AutoStart: false,
P2P_Enabled: false,
});
const setSmallConfig = vi.spyOn(runtime.services.config, "setSmallConfig").mockImplementation(() => undefined);
let finishWrite!: () => void;
const setValue = vi.spyOn(runtime.services.config, "setValue").mockImplementation(
() =>
new Promise<void>((resolve) => {
finishWrite = resolve;
})
);
const openDatabase = vi.spyOn(runtime.services.database, "openDatabase").mockImplementation(async () => {
expect(setSmallConfig).toHaveBeenCalledWith(SETTING_KEY_P2P_DEVICE_NAME, "p2p-check-browser-desktop-abc");
expect(setValue).toHaveBeenCalledWith(SETTING_KEY_P2P_DEVICE_NAME, "p2p-check-browser-desktop-abc");
return true;
});
await runtime.start();
const starting = runtime.start();
await vi.waitFor(() => expect(setValue).toHaveBeenCalledOnce());
expect(openDatabase).not.toHaveBeenCalled();
finishWrite();
await starting;
expect(runtime.services.API.getSystemVaultName()).toBe("p2p-check-vault");
expect(openDatabase).toHaveBeenCalledOnce();
});
it("reads the current device name after the pane updates the store", async () => {
const runtime = new WebPeerRuntime({
deviceValues: new MemoryValueStore(),
store: createMemoryStore(),
systemVaultName: "webpeer-vault",
});
vi.spyOn(runtime.services.setting, "currentSettings").mockReturnValue({ ...DEFAULT_SETTINGS });
expect(await runtime.getDeviceName()).toBe("webpeer-vault");
await runtime.services.config.setValue(SETTING_KEY_P2P_DEVICE_NAME, "first-name");
expect(await runtime.getDeviceName()).toBe("first-name");
await runtime.services.config.setValue(SETTING_KEY_P2P_DEVICE_NAME, "renamed-device");
expect(await runtime.getDeviceName()).toBe("renamed-device");
});
});