|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import type { SessionEntry } from "../config/sessions/types.js"; |
| 3 | +import { resolvePreferredSessionKeyForSessionIdMatches } from "./session-id-resolution.js"; |
| 4 | + |
| 5 | +function entry(updatedAt: number, sessionId = "s1"): SessionEntry { |
| 6 | + return { sessionId, updatedAt }; |
| 7 | +} |
| 8 | + |
| 9 | +describe("resolvePreferredSessionKeyForSessionIdMatches", () => { |
| 10 | + it("returns undefined for empty matches", () => { |
| 11 | + expect(resolvePreferredSessionKeyForSessionIdMatches([], "s1")).toBeUndefined(); |
| 12 | + }); |
| 13 | + |
| 14 | + it("returns the only match for a single-element array", () => { |
| 15 | + const matches: Array<[string, SessionEntry]> = [["agent:main:main", entry(10)]]; |
| 16 | + expect(resolvePreferredSessionKeyForSessionIdMatches(matches, "s1")).toBe("agent:main:main"); |
| 17 | + }); |
| 18 | + |
| 19 | + it("collapses alias duplicates before resolving structural ties", () => { |
| 20 | + const matches: Array<[string, SessionEntry]> = [ |
| 21 | + ["agent:main:MAIN", entry(10, "main")], |
| 22 | + ["agent:main:main", entry(10, "main")], |
| 23 | + ]; |
| 24 | + |
| 25 | + expect(resolvePreferredSessionKeyForSessionIdMatches(matches, "main")).toBe("agent:main:main"); |
| 26 | + }); |
| 27 | + |
| 28 | + it("returns the freshest match when timestamps differ", () => { |
| 29 | + const matches: Array<[string, SessionEntry]> = [ |
| 30 | + ["agent:main:alpha", entry(10)], |
| 31 | + ["agent:main:beta", entry(20)], |
| 32 | + ]; |
| 33 | + expect(resolvePreferredSessionKeyForSessionIdMatches(matches, "s1")).toBe("agent:main:beta"); |
| 34 | + }); |
| 35 | + |
| 36 | + it("returns undefined for fuzzy-only matches with tied timestamps", () => { |
| 37 | + const matches: Array<[string, SessionEntry]> = [ |
| 38 | + ["agent:main:beta", entry(10)], |
| 39 | + ["agent:main:alpha", entry(10)], |
| 40 | + ]; |
| 41 | + expect(resolvePreferredSessionKeyForSessionIdMatches(matches, "s1")).toBeUndefined(); |
| 42 | + }); |
| 43 | + |
| 44 | + it("prefers the freshest structural match over a fresher fuzzy match", () => { |
| 45 | + const matches: Array<[string, SessionEntry]> = [ |
| 46 | + ["agent:main:other", entry(999, "run-dup")], |
| 47 | + ["agent:main:acp:run-dup", entry(100, "run-dup")], |
| 48 | + ["agent:main:acp2:run-dup", entry(50, "run-dup")], |
| 49 | + ]; |
| 50 | + |
| 51 | + expect(resolvePreferredSessionKeyForSessionIdMatches(matches, "run-dup")).toBe( |
| 52 | + "agent:main:acp:run-dup", |
| 53 | + ); |
| 54 | + }); |
| 55 | + |
| 56 | + it("preserves ambiguity for distinct structural ties", () => { |
| 57 | + const matches: Array<[string, SessionEntry]> = [ |
| 58 | + ["agent:main:b:sid", entry(10, "sid")], |
| 59 | + ["agent:main:a:sid", entry(10, "sid")], |
| 60 | + ["agent:main:extra", entry(500, "sid")], |
| 61 | + ]; |
| 62 | + |
| 63 | + expect(resolvePreferredSessionKeyForSessionIdMatches(matches, "sid")).toBeUndefined(); |
| 64 | + }); |
| 65 | +}); |
0 commit comments