import { describe, expect, test } from "bun:test"; import { deviceListSignature, isNoDevicesSelected, resolveDeviceAutoSelect, } from "./device-selection"; describe("deviceListSignature", () => { test("is order-independent", () => { expect(deviceListSignature(["b", "a", "c"])).toBe( deviceListSignature(["c", "b", "a"]), ); }); test("empty list is the empty signature", () => { expect(deviceListSignature([])).toBe(""); }); }); describe("resolveDeviceAutoSelect", () => { test("selects all on a fresh load with nothing selected", () => { expect( resolveDeviceAutoSelect({ deviceIds: ["a", "b"], selectedCount: 0, initializedSignature: null, }), ).toEqual({ signature: "a,b", select: ["a", "b"] }); }); test("records signature without changing a URL-restored selection", () => { expect( resolveDeviceAutoSelect({ deviceIds: ["a", "b"], selectedCount: 1, initializedSignature: null, }), ).toEqual({ signature: "a,b", select: null }); }); test("does NOT re-select after the user cleared to None (same list)", () => { // This is the OPL-2983 bug: an empty selection whose list is already // initialized is a deliberate "None", not a fresh load. expect( resolveDeviceAutoSelect({ deviceIds: ["a", "b"], selectedCount: 0, initializedSignature: "a,b", }), ).toBeNull(); }); test("re-initializes when the device list changes (e.g. team switch)", () => { expect( resolveDeviceAutoSelect({ deviceIds: ["x", "y"], selectedCount: 0, initializedSignature: "a,b", }), ).toEqual({ signature: "x,y", select: ["x", "y"] }); }); test("no action for an empty device list", () => { expect( resolveDeviceAutoSelect({ deviceIds: [], selectedCount: 0, initializedSignature: null, }), ).toBeNull(); }); }); describe("isNoDevicesSelected", () => { test("true only after init when the user cleared everything", () => { expect( isNoDevicesSelected({ deviceCount: 2, selectedCount: 0, currentSignature: "a,b", initializedSignature: "a,b", }), ).toBe(true); }); test("false during the pre-auto-select first render", () => { expect( isNoDevicesSelected({ deviceCount: 2, selectedCount: 0, currentSignature: "a,b", initializedSignature: null, }), ).toBe(false); }); test("false when some devices are selected", () => { expect( isNoDevicesSelected({ deviceCount: 2, selectedCount: 1, currentSignature: "a,b", initializedSignature: "a,b", }), ).toBe(false); }); test("false when the list changed but selection not yet reconciled", () => { expect( isNoDevicesSelected({ deviceCount: 2, selectedCount: 0, currentSignature: "x,y", initializedSignature: "a,b", }), ).toBe(false); }); });