/** * @license * Copyright 2025-2026 Open Home Foundation * SPDX-License-Identifier: Apache-2.0 */ import { Environment, MemoryStorageDriver, MockStorageService, SupportedStorageTypes } from "@matter/general"; import { ConfigStorage } from "../src/server/ConfigStorage.js"; async function createConfig(): Promise { const env = new Environment("test"); new MockStorageService(env); return ConfigStorage.create(env); } /** Config storage that outlives one instance, so a reopen sees what the last one wrote. */ function persistentConfig() { const storage: Record> = {}; return async () => { const env = new Environment("test"); new MockStorageService(env, () => new MemoryStorageDriver(storage)); return { config: await ConfigStorage.create(env), storage }; }; } describe("ConfigStorage", () => { describe("allocateNodeId", () => { let config: ConfigStorage; beforeEach(async () => { config = await createConfig(); }); afterEach(async () => { await config.close(); }); it("returns the current counter and advances it", async () => { expect(config.nextNodeId).to.equal(1); const first = await config.allocateNodeId(() => false); expect(first).to.equal(1); expect(config.nextNodeId).to.equal(2); const second = await config.allocateNodeId(() => false); expect(second).to.equal(2); expect(config.nextNodeId).to.equal(3); }); it("skips ids that are already in use", async () => { const inUse = new Set([1, 2, 3]); const allocated = await config.allocateNodeId(id => inUse.has(id)); expect(allocated).to.equal(4); expect(config.nextNodeId).to.equal(5); }); it("serializes concurrent allocations so ids never collide", async () => { const results = await Promise.all(Array.from({ length: 10 }, () => config.allocateNodeId(() => false))); const unique = new Set(results.map(id => id.toString())); expect(unique.size).to.equal(results.length); expect([...unique].sort((a, b) => Number(a) - Number(b))).to.deep.equal( Array.from({ length: 10 }, (_, i) => String(i + 1)), ); expect(config.nextNodeId).to.equal(11); }); it("preserves the bigint counter type when advancing", async () => { await config.set({ nextNodeId: 100n }); const allocated = await config.allocateNodeId(() => false); expect(allocated).to.equal(100n); expect(config.nextNodeId).to.equal(101n); expect(typeof config.nextNodeId).to.equal("bigint"); }); }); describe("fabricLabel", () => { let config: ConfigStorage; beforeEach(async () => { config = await createConfig(); }); afterEach(async () => { await config.close(); }); it("defaults to HomeAssistant and is unlocked", () => { expect(config.fabricLabel).to.equal("HomeAssistant"); expect(config.fabricLabelLocked).to.be.false; }); it("locks the label and reports it as locked", async () => { await config.lockFabricLabel("Pinned"); expect(config.fabricLabel).to.equal("Pinned"); expect(config.fabricLabelLocked).to.be.true; }); it("cuts a pinned label to 32 UTF-8 bytes without splitting a character", async () => { await config.lockFabricLabel(" äääääääääääääää€ "); expect(config.fabricLabel).to.equal("äääääääääääääää"); }); it("drops a space the cut leaves at the end", async () => { await config.lockFabricLabel(`${"a".repeat(30)} üx`); expect(config.fabricLabel).to.equal("a".repeat(30)); }); it("cuts a label stored by an earlier version when it opens", async () => { const open = persistentConfig(); const first = await open(); await first.config.close(); first.storage["values"] = { ...first.storage["values"], fabricLabel: "Wohnzimmer Süd Erdgeschoss Küche" }; const second = await open(); try { expect(second.config.fabricLabel).to.equal("Wohnzimmer Süd Erdgeschoss Küc"); } finally { await second.config.close(); } }); }); describe("peer settings repair marker", () => { it("remembers each storage scope it was told about", async () => { const config = await createConfig(); try { expect(config.hasRepairedPeerSettings("server")).to.be.false; await config.markPeerSettingsRepaired("server"); await config.markPeerSettingsRepaired("server-2-fff1"); // Both, not just the most recent: a server started against the other fabric must not // repeat a repair that already ran for it. expect(config.hasRepairedPeerSettings("server")).to.be.true; expect(config.hasRepairedPeerSettings("server-2-fff1")).to.be.true; expect(config.hasRepairedPeerSettings("server-3-fff1")).to.be.false; } finally { await config.close(); } }); it("keeps the scopes across a restart", async () => { const open = persistentConfig(); const first = await open(); await first.config.markPeerSettingsRepaired("server"); await first.config.close(); const second = await open(); try { expect(second.config.hasRepairedPeerSettings("server")).to.be.true; } finally { await second.config.close(); } }); it("reads a scope stored by a version that recorded only one", async () => { const open = persistentConfig(); const first = await open(); await first.config.close(); // The shape written before this server could run against more than one fabric. first.storage["values"] = { ...first.storage["values"], peerSettingsRepairedFor: "server" }; const second = await open(); try { expect(second.config.hasRepairedPeerSettings("server")).to.be.true; } finally { await second.config.close(); } }); }); });