mirror of
https://github.com/Kilo-Org/kilocode.git
synced 2026-09-21 05:52:35 +08:00
67 lines
2.4 KiB
TypeScript
67 lines
2.4 KiB
TypeScript
import { describe, expect, it } from "bun:test"
|
|
import { createSessionVariants } from "../../webview-ui/src/context/session-variants"
|
|
import type { ExtensionMessage, ModelSelection } from "../../webview-ui/src/types/messages"
|
|
|
|
const model: ModelSelection = { providerID: "anthropic", modelID: "claude-sonnet-4" }
|
|
|
|
function setup(session?: string) {
|
|
const selections: Record<string, string> = {}
|
|
const messages: Array<{ type: string; key?: string; value?: string }> = []
|
|
const order: string[] = []
|
|
let handler: ((message: ExtensionMessage) => void) | undefined
|
|
const variants = createSessionVariants({
|
|
selections: () => selections,
|
|
set: (key, value) => {
|
|
selections[key] = value
|
|
},
|
|
selected: () => model,
|
|
session: () => session,
|
|
agent: () => "code",
|
|
find: () => ({ variants: { low: {}, high: {} } }),
|
|
post: (message) => {
|
|
order.push("post")
|
|
messages.push(message)
|
|
},
|
|
listen: (next) => {
|
|
order.push("listen")
|
|
handler = next
|
|
return () => order.push("unsub")
|
|
},
|
|
})
|
|
return { variants, selections, messages, order, dispatch: (message: ExtensionMessage) => handler?.(message) }
|
|
}
|
|
|
|
describe("session variants", () => {
|
|
it("subscribes before requesting persisted variants and returns cleanup", () => {
|
|
const state = setup()
|
|
const unsub = state.variants.load()
|
|
expect(state.order).toEqual(["listen", "post"])
|
|
expect(state.messages).toEqual([{ type: "requestVariants" }])
|
|
unsub()
|
|
expect(state.order).toEqual(["listen", "post", "unsub"])
|
|
})
|
|
|
|
it("loads global variants without restoring stale session variants", () => {
|
|
const state = setup()
|
|
state.variants.load()
|
|
state.dispatch({
|
|
type: "variantsLoaded",
|
|
variants: { "agent/code/anthropic/claude-sonnet-4": "high", "session/old/model": "low" },
|
|
})
|
|
expect(state.selections).toEqual({ "agent/code/anthropic/claude-sonnet-4": "high" })
|
|
})
|
|
|
|
it("persists global selections but keeps session selections local", () => {
|
|
const global = setup()
|
|
global.variants.select("high")
|
|
expect(global.messages).toEqual([
|
|
{ type: "persistVariant", key: "agent/code/anthropic/claude-sonnet-4", value: "high" },
|
|
])
|
|
|
|
const scoped = setup("session-a")
|
|
scoped.variants.select("low")
|
|
expect(scoped.selections).toEqual({ "session/session-a/anthropic/claude-sonnet-4": "low" })
|
|
expect(scoped.messages).toEqual([])
|
|
})
|
|
})
|