murashit_codex-panel/tests/app-server/shared-cache-state.test.ts
2026-06-10 12:18:47 +09:00

80 lines
3 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { createAppServerDiagnostics } from "../../src/app-server/diagnostics";
import { emptyRuntimeConfigSnapshot } from "../../src/app-server/runtime-config";
import {
applySharedAppServerMetadata,
applySharedModels,
applySharedThreadList,
cachedSharedAppServerMetadata,
cachedSharedModels,
cachedSharedThreadList,
createSharedAppServerState,
} from "../../src/app-server/shared-cache-state";
import type { ModelMetadata } from "../../src/domain/catalog/metadata";
import type { Thread } from "../../src/domain/threads/model";
describe("shared app-server cache state", () => {
it("keeps snapshots detached from caller-owned arrays", () => {
const sourceThreads = [threadFixture("thread-1")];
const threadState = applySharedThreadList(createSharedAppServerState(), sourceThreads);
sourceThreads.push(threadFixture("thread-2"));
const cachedThreads = cachedSharedThreadList(threadState);
expect(cachedThreads?.map((thread) => thread.id)).toEqual(["thread-1"]);
const mutableCachedThreads = cachedThreads as Thread[];
mutableCachedThreads.push(threadFixture("thread-3"));
expect(cachedSharedThreadList(threadState)?.map((thread) => thread.id)).toEqual(["thread-1"]);
const sourceModels = [modelFixture("gpt-5.5")];
const modelState = applySharedModels(createSharedAppServerState(), sourceModels);
sourceModels.push(modelFixture("gpt-5.6"));
expect(modelState.availableModels.map((model) => model.model)).toEqual(["gpt-5.5"]);
const cachedModels = cachedSharedModels(modelState);
(cachedModels[0]?.supportedReasoningEfforts as string[] | undefined)?.push("high");
expect(cachedSharedModels(modelState)[0]?.supportedReasoningEfforts).toEqual([]);
const metadataState = applySharedAppServerMetadata(createSharedAppServerState(), {
runtimeConfig: emptyRuntimeConfigSnapshot(),
availableModels: sourceModels,
availableSkills: [{ name: "skill", description: "", path: "/tmp/skill", enabled: true }],
rateLimit: null,
appServerDiagnostics: {
...createAppServerDiagnostics(),
mcpServers: [{ name: "server", startupStatus: "ready", authStatus: null, toolCount: 1, message: null }],
},
});
sourceModels.push(modelFixture("gpt-5.7"));
const cachedMetadata = cachedSharedAppServerMetadata(metadataState);
expect(cachedMetadata?.availableModels.map((model) => model.model)).toEqual(["gpt-5.5", "gpt-5.6"]);
});
});
function threadFixture(id: string): Thread {
return {
id,
preview: "",
name: null,
archived: false,
createdAt: 1,
updatedAt: 1,
};
}
function modelFixture(model: string): ModelMetadata {
return {
id: model,
model,
displayName: model,
description: "",
hidden: false,
supportedReasoningEfforts: [],
defaultReasoningEffort: "medium",
inputModalities: [],
additionalSpeedTiers: [],
serviceTiers: [],
defaultServiceTier: null,
isDefault: false,
};
}