test: use sqlite locators in gateway fixtures

This commit is contained in:
Peter Steinberger
2026-05-09 05:52:35 +01:00
parent 0102d207b4
commit d228e3379e
2 changed files with 34 additions and 13 deletions

View File

@@ -4,6 +4,7 @@ import type { AddressInfo } from "node:net";
import os from "node:os";
import path from "node:path";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { createSqliteSessionTranscriptLocator } from "../config/sessions/paths.js";
import { replaceSqliteSessionTranscriptEvents } from "../config/sessions/transcript-store.sqlite.js";
import { createPinnedLookup } from "../infra/net/ssrf.js";
import { resolvePreferredOpenClawTmpDir } from "../infra/tmp-openclaw-dir.js";
@@ -20,6 +21,10 @@ const readSessionMessagesMock = vi.fn();
const expectedManagedOriginalsDir = () =>
path.join(resolvePreferredOpenClawTmpDir(), "media", "outgoing", "originals");
function sqliteTranscript(sessionId: string, agentId = "main"): string {
return createSqliteSessionTranscriptLocator({ agentId, sessionId });
}
vi.mock("./http-utils.js", () => ({
authorizeGatewayHttpRequestOrReply: authorizeGatewayHttpRequestOrReplyMock,
resolveOpenAiCompatibleHttpOperatorScopes: resolveOpenAiCompatibleHttpOperatorScopesMock,
@@ -167,7 +172,10 @@ async function requestManagedImage(params: {
});
loadSessionEntryMock.mockReturnValue({
storePath: path.join(params.stateDir, "openclaw-state.sqlite"),
entry: params.sessionEntry ?? { sessionId: "sess-1", sessionFile: "session.jsonl" },
entry: params.sessionEntry ?? {
sessionId: "sess-1",
sessionFile: sqliteTranscript("sess-1"),
},
});
readSessionMessagesMock.mockReturnValue(
params.transcriptMessages ?? [
@@ -380,7 +388,7 @@ describe("handleManagedOutgoingImageHttpRequest", () => {
it("reuses the session attachment index across requests until the transcript changes", async () => {
const { attachmentId, sessionKey } = await createFixture(stateDir);
const sessionFile = path.join(stateDir, "agents", "main", "sessions", "sess-main.jsonl");
const sessionFile = sqliteTranscript("sess-main");
replaceSqliteSessionTranscriptEvents({
agentId: "main",
sessionId: "sess-main",
@@ -953,7 +961,7 @@ describe("cleanupManagedOutgoingImageRecords", () => {
const fixture = await createFixture(stateDir);
loadSessionEntryMock.mockReturnValue({
storePath: path.join(stateDir, "openclaw-state.sqlite"),
entry: { sessionId: "sess-main", sessionFile: "/tmp/sess-main.jsonl" },
entry: { sessionId: "sess-main", sessionFile: sqliteTranscript("sess-main") },
});
readSessionMessagesMock.mockReturnValue([]);
@@ -971,7 +979,7 @@ describe("cleanupManagedOutgoingImageRecords", () => {
const fixture = await createFixture(stateDir);
loadSessionEntryMock.mockReturnValue({
storePath: path.join(stateDir, "openclaw-state.sqlite"),
entry: { sessionId: "sess-main", sessionFile: "/tmp/sess-main.jsonl" },
entry: { sessionId: "sess-main", sessionFile: sqliteTranscript("sess-main") },
});
readSessionMessagesMock.mockReturnValue([
{
@@ -1007,7 +1015,7 @@ describe("cleanupManagedOutgoingImageRecords", () => {
});
loadSessionEntryMock.mockReturnValue({
storePath: path.join(stateDir, "openclaw-state.sqlite"),
entry: { sessionId: "sess-main", sessionFile: "/tmp/sess-main.jsonl" },
entry: { sessionId: "sess-main", sessionFile: sqliteTranscript("sess-main") },
});
readSessionMessagesMock.mockReturnValue([
{
@@ -1051,7 +1059,10 @@ describe("cleanupManagedOutgoingImageRecords", () => {
storePath: path.join(stateDir, "openclaw-state.sqlite"),
entry: {
sessionId: sessionKey === retainedFixture.sessionKey ? "sess-other" : "sess-main",
sessionFile: "/tmp/session.jsonl",
sessionFile:
sessionKey === retainedFixture.sessionKey
? sqliteTranscript("sess-other", "other")
: sqliteTranscript("sess-main"),
},
}));
readSessionMessagesMock.mockReturnValue([]);

View File

@@ -32,6 +32,8 @@ vi.mock("../../infra/session-cost-usage.js", async () => {
const actual = await vi.importActual<typeof import("../../infra/session-cost-usage.js")>(
"../../infra/session-cost-usage.js",
);
const locator = (agentId: string, sessionId: string) =>
`sqlite-transcript://${agentId}/${sessionId}.jsonl`;
return {
...actual,
discoverAllSessions: vi.fn(async (params?: { agentId?: string }) => {
@@ -39,7 +41,7 @@ vi.mock("../../infra/session-cost-usage.js", async () => {
return [
{
sessionId: "s-main",
sessionFile: "/tmp/transcript-fixtures/main/s-main.jsonl",
sessionFile: locator("main", "s-main"),
mtime: 100,
firstUserMessage: "hello",
},
@@ -49,7 +51,7 @@ vi.mock("../../infra/session-cost-usage.js", async () => {
return [
{
sessionId: "s-opus",
sessionFile: "/tmp/transcript-fixtures/opus/s-opus.jsonl",
sessionFile: locator("opus", "s-opus"),
mtime: 200,
firstUserMessage: "hi",
},
@@ -178,10 +180,14 @@ describe("sessions.usage", () => {
try {
await withEnvAsync({ OPENCLAW_STATE_DIR: stateDir }, async () => {
const sessionFile = createSqliteSessionTranscriptLocator({
agentId: "opus",
sessionId: "s-opus",
});
replaceSqliteSessionTranscriptEvents({
agentId: "opus",
sessionId: "s-opus",
transcriptPath: path.join(stateDir, "agents", "opus", "sessions", "s-opus.jsonl"),
transcriptPath: sessionFile,
events: [{ type: "session", id: "s-opus" }],
});
// Swap the store mock for this test: the canonical key differs from the discovered key
@@ -191,7 +197,7 @@ describe("sessions.usage", () => {
entries: {
[storeKey]: {
sessionId: "s-opus",
sessionFile: "s-opus.jsonl",
sessionFile,
label: "Named session",
updatedAt: 999,
},
@@ -330,10 +336,14 @@ describe("sessions.usage", () => {
try {
await withEnvAsync({ OPENCLAW_STATE_DIR: stateDir }, async () => {
const sessionFile = createSqliteSessionTranscriptLocator({
agentId: "opus",
sessionId: "run-dup",
});
replaceSqliteSessionTranscriptEvents({
agentId: "opus",
sessionId: "run-dup",
transcriptPath: path.join(stateDir, "agents", "opus", "sessions", "run-dup.jsonl"),
transcriptPath: sessionFile,
events: [{ type: "session", id: "run-dup" }],
});
vi.mocked(loadCombinedSessionEntriesForGateway).mockReturnValue({
@@ -341,12 +351,12 @@ describe("sessions.usage", () => {
entries: {
[preferredKey]: {
sessionId: "run-dup",
sessionFile: "run-dup.jsonl",
sessionFile,
updatedAt: 1_000,
},
"agent:other:main": {
sessionId: "run-dup",
sessionFile: "run-dup.jsonl",
sessionFile,
updatedAt: 2_000,
},
},