test(sessions): add delivery info regression coverage

This commit is contained in:
Sebastian
2026-02-17 09:59:49 -05:00
parent c0072be6a6
commit 3f66280c3c
2 changed files with 95 additions and 0 deletions

View File

@@ -0,0 +1,94 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import type { SessionEntry } from "./types.js";
const storeState = vi.hoisted(() => ({
store: {} as Record<string, SessionEntry>,
}));
vi.mock("../io.js", () => ({
loadConfig: () => ({}),
}));
vi.mock("./paths.js", () => ({
resolveStorePath: () => "/tmp/sessions.json",
}));
vi.mock("./store.js", () => ({
loadSessionStore: () => storeState.store,
}));
import { extractDeliveryInfo } from "./delivery-info.js";
const buildEntry = (deliveryContext: SessionEntry["deliveryContext"]): SessionEntry => ({
sessionId: "session-1",
updatedAt: Date.now(),
deliveryContext,
});
beforeEach(() => {
storeState.store = {};
});
describe("extractDeliveryInfo", () => {
it("returns deliveryContext for direct session keys", () => {
const sessionKey = "agent:main:webchat:dm:user-123";
storeState.store[sessionKey] = buildEntry({
channel: "webchat",
to: "webchat:user-123",
accountId: "default",
});
const result = extractDeliveryInfo(sessionKey);
expect(result).toEqual({
deliveryContext: {
channel: "webchat",
to: "webchat:user-123",
accountId: "default",
},
threadId: undefined,
});
});
it("falls back to base sessions for :thread: keys", () => {
const baseKey = "agent:main:slack:channel:C0123ABC";
const threadKey = `${baseKey}:thread:1234567890.123456`;
storeState.store[baseKey] = buildEntry({
channel: "slack",
to: "slack:C0123ABC",
accountId: "workspace-1",
});
const result = extractDeliveryInfo(threadKey);
expect(result).toEqual({
deliveryContext: {
channel: "slack",
to: "slack:C0123ABC",
accountId: "workspace-1",
},
threadId: "1234567890.123456",
});
});
it("falls back to base sessions for :topic: keys", () => {
const baseKey = "agent:main:telegram:group:98765";
const topicKey = `${baseKey}:topic:55`;
storeState.store[baseKey] = buildEntry({
channel: "telegram",
to: "group:98765",
accountId: "main",
});
const result = extractDeliveryInfo(topicKey);
expect(result).toEqual({
deliveryContext: {
channel: "telegram",
to: "group:98765",
accountId: "main",
},
threadId: "55",
});
});
});