test(session): cover stale threadId fallback

This commit is contained in:
Sebastian
2026-02-16 22:08:51 -05:00
parent 7a00f056af
commit def0254169
2 changed files with 52 additions and 1 deletions

View File

@@ -2,8 +2,8 @@ import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import { buildModelAliasIndex } from "../../agents/model-selection.js";
import type { OpenClawConfig } from "../../config/config.js";
import { buildModelAliasIndex } from "../../agents/model-selection.js";
import { saveSessionStore } from "../../config/sessions.js";
import { formatZonedTimestamp } from "../../infra/format-time/format-datetime.ts";
import { enqueueSystemEvent, resetSystemEventsForTest } from "../../infra/system-events.js";
@@ -1294,6 +1294,54 @@ describe("persistSessionUsageUpdate", () => {
});
describe("initSessionState stale threadId fallback", () => {
async function seedSessionStore(params: {
storePath: string;
sessionKey: string;
entry: Record<string, unknown>;
}) {
await fs.mkdir(path.dirname(params.storePath), { recursive: true });
await fs.writeFile(
params.storePath,
JSON.stringify({ [params.sessionKey]: params.entry }, null, 2),
"utf-8",
);
}
it("ignores persisted lastThreadId on main sessions for non-thread messages", async () => {
const storePath = await createStorePath("stale-main-thread-");
const sessionKey = "agent:main:main";
await seedSessionStore({
storePath,
sessionKey,
entry: {
sessionId: "s1",
updatedAt: Date.now(),
lastChannel: "telegram",
lastTo: "telegram:123",
lastThreadId: 42,
deliveryContext: {
channel: "telegram",
to: "telegram:123",
threadId: 42,
},
},
});
const cfg = { session: { store: storePath } } as OpenClawConfig;
const result = await initSessionState({
ctx: {
Body: "hello from DM",
SessionKey: sessionKey,
},
cfg,
commandAuthorized: true,
});
expect(result.sessionEntry.lastThreadId).toBeUndefined();
expect(result.sessionEntry.deliveryContext?.threadId).toBeUndefined();
});
it("does not inherit lastThreadId from a previous thread interaction in non-thread sessions", async () => {
const storePath = await createStorePath("stale-thread-");
const cfg = { session: { store: storePath } } as OpenClawConfig;