|
| 1 | +/** |
| 2 | + * Tests for cortex-sync configuration parsing, including P2P bridge (B3). |
| 3 | + */ |
| 4 | + |
| 5 | +import { describe, expect, it } from "vitest"; |
| 6 | +import { parseCortexSyncConfig, type CortexSyncConfig } from "./config.js"; |
| 7 | + |
| 8 | +describe("parseCortexSyncConfig", () => { |
| 9 | + it("returns defaults for empty input", () => { |
| 10 | + const cfg = parseCortexSyncConfig({}); |
| 11 | + expect(cfg.namespace).toBe("mayros"); |
| 12 | + expect(cfg.sync.intervalSeconds).toBe(300); |
| 13 | + expect(cfg.sync.autoSync).toBe(false); |
| 14 | + expect(cfg.sync.conflictStrategy).toBe("last-writer-wins"); |
| 15 | + expect(cfg.sync.maxTriplesPerSync).toBe(5000); |
| 16 | + expect(cfg.sync.syncTimeoutMs).toBe(30000); |
| 17 | + expect(cfg.sync.nativeP2pPreferred).toBe(true); |
| 18 | + expect(cfg.discovery.bonjourEnabled).toBe(false); |
| 19 | + expect(cfg.discovery.manualPeers).toEqual([]); |
| 20 | + }); |
| 21 | + |
| 22 | + it("nativeP2pPreferred defaults to true", () => { |
| 23 | + const cfg = parseCortexSyncConfig({}); |
| 24 | + expect(cfg.sync.nativeP2pPreferred).toBe(true); |
| 25 | + }); |
| 26 | + |
| 27 | + it("nativeP2pPreferred can be set to false", () => { |
| 28 | + const cfg = parseCortexSyncConfig({ |
| 29 | + sync: { nativeP2pPreferred: false }, |
| 30 | + }); |
| 31 | + expect(cfg.sync.nativeP2pPreferred).toBe(false); |
| 32 | + }); |
| 33 | + |
| 34 | + it("nativeP2pPreferred true is respected", () => { |
| 35 | + const cfg = parseCortexSyncConfig({ |
| 36 | + sync: { nativeP2pPreferred: true }, |
| 37 | + }); |
| 38 | + expect(cfg.sync.nativeP2pPreferred).toBe(true); |
| 39 | + }); |
| 40 | + |
| 41 | + it("clamps intervalSeconds to range", () => { |
| 42 | + const cfg1 = parseCortexSyncConfig({ sync: { intervalSeconds: 1 } }); |
| 43 | + expect(cfg1.sync.intervalSeconds).toBe(10); |
| 44 | + |
| 45 | + const cfg2 = parseCortexSyncConfig({ sync: { intervalSeconds: 100000 } }); |
| 46 | + expect(cfg2.sync.intervalSeconds).toBe(86400); |
| 47 | + }); |
| 48 | + |
| 49 | + it("clamps maxTriplesPerSync to range", () => { |
| 50 | + const cfg1 = parseCortexSyncConfig({ sync: { maxTriplesPerSync: 1 } }); |
| 51 | + expect(cfg1.sync.maxTriplesPerSync).toBe(100); |
| 52 | + |
| 53 | + const cfg2 = parseCortexSyncConfig({ sync: { maxTriplesPerSync: 100000 } }); |
| 54 | + expect(cfg2.sync.maxTriplesPerSync).toBe(50000); |
| 55 | + }); |
| 56 | + |
| 57 | + it("parses valid conflictStrategy", () => { |
| 58 | + const cfg = parseCortexSyncConfig({ |
| 59 | + sync: { conflictStrategy: "keep-both" }, |
| 60 | + }); |
| 61 | + expect(cfg.sync.conflictStrategy).toBe("keep-both"); |
| 62 | + }); |
| 63 | + |
| 64 | + it("falls back to default for invalid conflictStrategy", () => { |
| 65 | + const cfg = parseCortexSyncConfig({ |
| 66 | + sync: { conflictStrategy: "invalid" }, |
| 67 | + }); |
| 68 | + expect(cfg.sync.conflictStrategy).toBe("last-writer-wins"); |
| 69 | + }); |
| 70 | + |
| 71 | + it("parses manual peers", () => { |
| 72 | + const cfg = parseCortexSyncConfig({ |
| 73 | + discovery: { |
| 74 | + manualPeers: [ |
| 75 | + { |
| 76 | + nodeId: "node1", |
| 77 | + endpoint: "http://localhost:8080", |
| 78 | + namespaces: ["ns1"], |
| 79 | + enabled: true, |
| 80 | + }, |
| 81 | + ], |
| 82 | + }, |
| 83 | + }); |
| 84 | + expect(cfg.discovery.manualPeers).toHaveLength(1); |
| 85 | + expect(cfg.discovery.manualPeers[0].nodeId).toBe("node1"); |
| 86 | + }); |
| 87 | + |
| 88 | + it("filters invalid manual peers (missing nodeId or endpoint)", () => { |
| 89 | + const cfg = parseCortexSyncConfig({ |
| 90 | + discovery: { |
| 91 | + manualPeers: [ |
| 92 | + { nodeId: "", endpoint: "http://localhost:8080" }, |
| 93 | + { nodeId: "valid", endpoint: "" }, |
| 94 | + { nodeId: "ok", endpoint: "http://valid" }, |
| 95 | + ], |
| 96 | + }, |
| 97 | + }); |
| 98 | + expect(cfg.discovery.manualPeers).toHaveLength(1); |
| 99 | + expect(cfg.discovery.manualPeers[0].nodeId).toBe("ok"); |
| 100 | + }); |
| 101 | + |
| 102 | + it("rejects unknown sync keys", () => { |
| 103 | + expect(() => parseCortexSyncConfig({ sync: { bogus: true } })).toThrow("unknown keys"); |
| 104 | + }); |
| 105 | +}); |
0 commit comments