|
| 1 | +// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import { describe, expect, it, vi } from "vitest"; |
| 5 | + |
| 6 | +import type { SandboxEntry } from "./registry"; |
| 7 | +import { |
| 8 | + backfillMessagingChannels, |
| 9 | + findAllOverlaps, |
| 10 | + findChannelConflicts, |
| 11 | +} from "./messaging-conflict"; |
| 12 | + |
| 13 | +function makeRegistry(sandboxes: SandboxEntry[]) { |
| 14 | + const store = new Map(sandboxes.map((s) => [s.name, { ...s }])); |
| 15 | + return { |
| 16 | + listSandboxes: () => ({ |
| 17 | + sandboxes: Array.from(store.values()), |
| 18 | + defaultSandbox: sandboxes[0]?.name ?? null, |
| 19 | + }), |
| 20 | + updateSandbox: vi.fn((name: string, updates: Partial<SandboxEntry>) => { |
| 21 | + const entry = store.get(name); |
| 22 | + if (!entry) return false; |
| 23 | + Object.assign(entry, updates); |
| 24 | + return true; |
| 25 | + }), |
| 26 | + }; |
| 27 | +} |
| 28 | + |
| 29 | +describe("findChannelConflicts", () => { |
| 30 | + it("returns conflicts when another sandbox already has the channel", () => { |
| 31 | + const registry = makeRegistry([ |
| 32 | + { name: "alice", messagingChannels: ["telegram"] }, |
| 33 | + { name: "bob", messagingChannels: [] }, |
| 34 | + ]); |
| 35 | + expect(findChannelConflicts("bob", ["telegram"], registry)).toEqual([ |
| 36 | + { channel: "telegram", sandbox: "alice" }, |
| 37 | + ]); |
| 38 | + }); |
| 39 | + |
| 40 | + it("excludes the current sandbox from its own conflicts", () => { |
| 41 | + const registry = makeRegistry([{ name: "alice", messagingChannels: ["telegram"] }]); |
| 42 | + expect(findChannelConflicts("alice", ["telegram"], registry)).toEqual([]); |
| 43 | + }); |
| 44 | + |
| 45 | + it("skips entries with no messagingChannels field (pre-backfill)", () => { |
| 46 | + const registry = makeRegistry([{ name: "alice" }, { name: "bob", messagingChannels: [] }]); |
| 47 | + expect(findChannelConflicts("bob", ["telegram"], registry)).toEqual([]); |
| 48 | + }); |
| 49 | + |
| 50 | + it("returns empty when no channels are enabled", () => { |
| 51 | + const registry = makeRegistry([{ name: "alice", messagingChannels: ["telegram"] }]); |
| 52 | + expect(findChannelConflicts("bob", [], registry)).toEqual([]); |
| 53 | + }); |
| 54 | +}); |
| 55 | + |
| 56 | +describe("findAllOverlaps", () => { |
| 57 | + it("reports each overlapping pair once", () => { |
| 58 | + const registry = makeRegistry([ |
| 59 | + { name: "alice", messagingChannels: ["telegram"] }, |
| 60 | + { name: "bob", messagingChannels: ["telegram"] }, |
| 61 | + { name: "carol", messagingChannels: ["discord"] }, |
| 62 | + ]); |
| 63 | + expect(findAllOverlaps(registry)).toEqual([ |
| 64 | + { channel: "telegram", sandboxes: ["alice", "bob"] }, |
| 65 | + ]); |
| 66 | + }); |
| 67 | + |
| 68 | + it("reports all pairs when three sandboxes share a channel", () => { |
| 69 | + const registry = makeRegistry([ |
| 70 | + { name: "a", messagingChannels: ["telegram"] }, |
| 71 | + { name: "b", messagingChannels: ["telegram"] }, |
| 72 | + { name: "c", messagingChannels: ["telegram"] }, |
| 73 | + ]); |
| 74 | + expect(findAllOverlaps(registry)).toEqual([ |
| 75 | + { channel: "telegram", sandboxes: ["a", "b"] }, |
| 76 | + { channel: "telegram", sandboxes: ["a", "c"] }, |
| 77 | + { channel: "telegram", sandboxes: ["b", "c"] }, |
| 78 | + ]); |
| 79 | + }); |
| 80 | + |
| 81 | + it("returns empty when channels do not overlap", () => { |
| 82 | + const registry = makeRegistry([ |
| 83 | + { name: "alice", messagingChannels: ["telegram"] }, |
| 84 | + { name: "bob", messagingChannels: ["discord"] }, |
| 85 | + ]); |
| 86 | + expect(findAllOverlaps(registry)).toEqual([]); |
| 87 | + }); |
| 88 | +}); |
| 89 | + |
| 90 | +describe("backfillMessagingChannels", () => { |
| 91 | + it("fills in missing messagingChannels by probing OpenShell", () => { |
| 92 | + const registry = makeRegistry([{ name: "alice" }]); |
| 93 | + const probe = { |
| 94 | + providerExists: vi.fn((name: string) => |
| 95 | + name === "alice-telegram-bridge" ? "present" : "absent", |
| 96 | + ) as (name: string) => "present" | "absent" | "error", |
| 97 | + }; |
| 98 | + backfillMessagingChannels(registry, probe); |
| 99 | + expect(registry.updateSandbox).toHaveBeenCalledWith("alice", { |
| 100 | + messagingChannels: ["telegram"], |
| 101 | + }); |
| 102 | + expect(probe.providerExists).toHaveBeenCalledWith("alice-telegram-bridge"); |
| 103 | + expect(probe.providerExists).toHaveBeenCalledWith("alice-discord-bridge"); |
| 104 | + expect(probe.providerExists).toHaveBeenCalledWith("alice-slack-bridge"); |
| 105 | + }); |
| 106 | + |
| 107 | + it("leaves entries with existing messagingChannels alone", () => { |
| 108 | + const registry = makeRegistry([ |
| 109 | + { name: "alice", messagingChannels: ["telegram"] }, |
| 110 | + ]); |
| 111 | + const probe = { |
| 112 | + providerExists: vi.fn(() => "present") as (name: string) => "present" | "absent" | "error", |
| 113 | + }; |
| 114 | + backfillMessagingChannels(registry, probe); |
| 115 | + expect(registry.updateSandbox).not.toHaveBeenCalled(); |
| 116 | + expect(probe.providerExists).not.toHaveBeenCalled(); |
| 117 | + }); |
| 118 | + |
| 119 | + it("writes an empty array when all probes return absent", () => { |
| 120 | + const registry = makeRegistry([{ name: "alice" }]); |
| 121 | + const probe = { |
| 122 | + providerExists: vi.fn(() => "absent") as (name: string) => "present" | "absent" | "error", |
| 123 | + }; |
| 124 | + backfillMessagingChannels(registry, probe); |
| 125 | + expect(registry.updateSandbox).toHaveBeenCalledWith("alice", { messagingChannels: [] }); |
| 126 | + }); |
| 127 | + |
| 128 | + it("does NOT persist when a probe returns error (retry on next call)", () => { |
| 129 | + // "error" is distinct from "absent": a transient gateway failure must not |
| 130 | + // be collapsed into "provider not attached" and persisted, because that |
| 131 | + // would prevent all future backfill retries and hide real overlaps. |
| 132 | + const registry = makeRegistry([{ name: "alice" }]); |
| 133 | + const probe = { |
| 134 | + providerExists: vi.fn((name: string) => { |
| 135 | + if (name.endsWith("-telegram-bridge")) return "error"; |
| 136 | + return name.endsWith("-discord-bridge") ? "present" : "absent"; |
| 137 | + }) as (name: string) => "present" | "absent" | "error", |
| 138 | + }; |
| 139 | + backfillMessagingChannels(registry, probe); |
| 140 | + expect(registry.updateSandbox).not.toHaveBeenCalled(); |
| 141 | + }); |
| 142 | + |
| 143 | + it("also treats a thrown probe as error (defensive; callers should return 'error' instead)", () => { |
| 144 | + const registry = makeRegistry([{ name: "alice" }]); |
| 145 | + const probe = { |
| 146 | + providerExists: vi.fn(() => { |
| 147 | + throw new Error("unexpected"); |
| 148 | + }) as (name: string) => "present" | "absent" | "error", |
| 149 | + }; |
| 150 | + backfillMessagingChannels(registry, probe); |
| 151 | + expect(registry.updateSandbox).not.toHaveBeenCalled(); |
| 152 | + }); |
| 153 | + |
| 154 | + it("re-attempts backfill on a subsequent call after a prior error", () => { |
| 155 | + const registry = makeRegistry([{ name: "alice" }]); |
| 156 | + let firstPass = true; |
| 157 | + const probe = { |
| 158 | + providerExists: vi.fn((name: string) => { |
| 159 | + if (name.endsWith("-telegram-bridge") && firstPass) { |
| 160 | + firstPass = false; |
| 161 | + return "error"; |
| 162 | + } |
| 163 | + return name === "alice-telegram-bridge" ? "present" : "absent"; |
| 164 | + }) as (name: string) => "present" | "absent" | "error", |
| 165 | + }; |
| 166 | + backfillMessagingChannels(registry, probe); |
| 167 | + expect(registry.updateSandbox).not.toHaveBeenCalled(); |
| 168 | + backfillMessagingChannels(registry, probe); |
| 169 | + expect(registry.updateSandbox).toHaveBeenCalledWith("alice", { |
| 170 | + messagingChannels: ["telegram"], |
| 171 | + }); |
| 172 | + }); |
| 173 | +}); |
0 commit comments