test: cover main last-channel routing

This commit is contained in:
Peter Steinberger
2025-12-12 16:35:47 +00:00
parent 00336f554f
commit 9eda40234f
2 changed files with 181 additions and 1 deletions

View File

@@ -1,6 +1,14 @@
import os from "node:os";
import path from "node:path";
import fs from "node:fs/promises";
import { describe, expect, it } from "vitest";
import { deriveSessionKey, resolveSessionKey } from "./sessions.js";
import {
deriveSessionKey,
loadSessionStore,
resolveSessionKey,
updateLastRoute,
} from "./sessions.js";
describe("sessions", () => {
it("returns normalized per-sender key", () => {
@@ -52,4 +60,38 @@ describe("sessions", () => {
resolveSessionKey("per-sender", { From: "12345-678@g.us" }, "main"),
).toBe("group:12345-678@g.us");
});
it("updateLastRoute persists channel and target", async () => {
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "clawdis-sessions-"));
const storePath = path.join(dir, "sessions.json");
await fs.writeFile(
storePath,
JSON.stringify(
{
main: {
sessionId: "sess-1",
updatedAt: 123,
systemSent: true,
thinkingLevel: "low",
},
},
null,
2,
),
"utf-8",
);
await updateLastRoute({
storePath,
sessionKey: "main",
channel: "telegram",
to: " 12345 ",
});
const store = loadSessionStore(storePath);
expect(store.main?.sessionId).toBe("sess-1");
expect(store.main?.updatedAt).toBeGreaterThanOrEqual(123);
expect(store.main?.lastChannel).toBe("telegram");
expect(store.main?.lastTo).toBe("12345");
});
});