import { test, expect, describe } from "bun:test"; import { primaryCluster, compareNodesByCluster } from "../src/lib/clusters"; import type { ClusterNode } from "../src/types"; import { clustersIntersect } from "../src/types"; const node = (id: string, clusters?: string[] | null): ClusterNode => ({ id, baseUrl: "http://x", platform: "ios", status: "online", lastSeenAt: 0, evalInFlight: 0, evalCapacity: 3, fixInFlight: 0, fixCapacity: 1, version: null, createdAt: 0, clusters: clusters ?? undefined, } as ClusterNode); const order = (ns: ClusterNode[]) => [...ns].sort(compareNodesByCluster).map((n) => n.id); describe("primaryCluster", () => { test("default wins over any other tag it is combined with", () => { // A node serving the baseline pool belongs with the baseline group no matter // what else it also carries, otherwise it drifts away from its pool-mates. expect(primaryCluster(node("a", ["im-sg", "default"]))).toBe("default"); expect(primaryCluster(node("b", ["default"]))).toBe("default"); }); test("otherwise the alphabetically first tag, so the group is stable", () => { expect(primaryCluster(node("a", ["pns", "im-sg"]))).toBe("im-sg"); }); test("a parked node has none", () => { expect(primaryCluster(node("a", []))).toBeNull(); expect(primaryCluster(node("a", null))).toBeNull(); }); }); describe("compareNodesByCluster", () => { test("default group comes first even though 'd' sorts after nothing special", () => { // 'im-sg' < 'pns' alphabetically, and both would outrank 'default' under a // plain string sort in some collations — default is pinned, not sorted. expect(order([ node("z-im", ["im-sg"]), node("a-pns", ["pns"]), node("m-def", ["default"]), ])).toEqual(["m-def", "z-im", "a-pns"]); }); test("named groups follow in alphabetical order", () => { expect(order([ node("n1", ["pns"]), node("n2", ["im-sg"]), node("n3", ["default"]), ])).toEqual(["n3", "n2", "n1"]); }); test("inside a group, node ids sort numerically", () => { // fei-ios-10 must not precede fei-ios-2 the way a plain string sort would. expect(order([ node("fei-ios-10", ["default"]), node("fei-ios-2", ["default"]), node("fei-ios-1", ["default"]), ])).toEqual(["fei-ios-1", "fei-ios-2", "fei-ios-10"]); }); test("parked nodes sink to the end", () => { expect(order([ node("parked", []), node("named", ["pns"]), node("base", ["default"]), ])).toEqual(["base", "named", "parked"]); }); test("matches the real fleet's expected layout", () => { const fleet = [ node("pns-android-01.shiro0.icu", ["pns"]), node("fei-ios-02.shiro0.icu", ["default"]), node("messaging-sg", ["im-sg"]), node("fei-android-01.shiro0.icu", ["default"]), node("messaging-sg-ios", ["im-sg"]), node("temp-ios-01", ["default"]), node("fei-ios-01.shiro0.icu", ["default"]), ]; expect(order(fleet)).toEqual([ // default pool, by name "fei-android-01.shiro0.icu", "fei-ios-01.shiro0.icu", "fei-ios-02.shiro0.icu", "temp-ios-01", // then #im-sg "messaging-sg", "messaging-sg-ios", // then #pns "pns-android-01.shiro0.icu", ]); }); test("is a total order — sorting is stable under input shuffling", () => { const fleet = [ node("b", ["pns"]), node("a", ["default"]), node("c", []), node("d", ["im-sg"]), ]; const once = order(fleet); expect(order([...fleet].reverse())).toEqual(once); }); }); /** * The pool rule the capability strip depends on. * * "default" is a real tag, not the absence of one: an untagged task is mapped to * `{default}` by requiredClustersOf, so it runs on `#default` nodes — while a * node carrying NO tags is parked and runs nothing. Reading that backwards makes * the untagged pool look empty, which is how the capability endpoint first * reported `nodes: 0` for the pool that has three machines in it. */ describe("clustersIntersect — which pool an untagged task lands on", () => { const untagged = { clusters: [] }; test("an untagged task runs on the #default pool", () => { expect(clustersIntersect(["default"], untagged)).toBe(true); expect(clustersIntersect(["default", "im-sg"], untagged)).toBe(true); }); test("a parked node (no tags) runs nothing at all", () => { expect(clustersIntersect([], untagged)).toBe(false); expect(clustersIntersect(null, { clusters: ["im-sg"] })).toBe(false); }); test("a tagged task stays inside its own pool", () => { expect(clustersIntersect(["im-sg"], { clusters: ["im-sg"] })).toBe(true); expect(clustersIntersect(["default"], { clusters: ["im-sg"] })).toBe(false); }); });