File size: 2,079 Bytes
3201ca6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { QueryClient } from "@tanstack/react-query";
import { beforeEach, describe, expect, it, vi } from "vitest";
import type { ActionEvent } from "#/types/agent-server/core/events/action-event";
import { handleActionEventCacheInvalidation } from "#/utils/cache-utils";
import { useModelStore } from "#/stores/model-store";

const makeActionEvent = (overrides: Partial<ActionEvent>): ActionEvent =>
  ({
    id: "ev-1",
    timestamp: new Date().toISOString(),
    source: "agent",
    tool_name: "SwitchLLMTool",
    tool_call_id: "call-1",
    action: { kind: "SwitchLLMAction" },
    ...overrides,
  }) as unknown as ActionEvent;

describe("handleActionEventCacheInvalidation", () => {
  beforeEach(() => {
    useModelStore.setState({
      entriesByConversation: {},
      activeProfileByConversation: {},
    });
  });

  it("refreshes the conversation and drops the optimistic profile when SwitchLLMTool fires", () => {
    useModelStore.setState({
      activeProfileByConversation: { "conv-1": "haiku" },
    });
    const queryClient = new QueryClient();
    const spy = vi.spyOn(queryClient, "invalidateQueries");

    handleActionEventCacheInvalidation(
      makeActionEvent({ tool_name: "SwitchLLMTool" }),
      "conv-1",
      queryClient,
    );

    expect(spy).toHaveBeenCalledWith({
      queryKey: ["user", "conversation", "conv-1"],
    });
    expect(
      useModelStore.getState().activeProfileByConversation["conv-1"],
    ).toBeUndefined();
  });

  it("does not touch the conversation cache for unrelated tool events", () => {
    const queryClient = new QueryClient();
    const spy = vi.spyOn(queryClient, "invalidateQueries");

    handleActionEventCacheInvalidation(
      makeActionEvent({ tool_name: "terminal" }),
      "conv-1",
      queryClient,
    );

    const conversationInvalidations = spy.mock.calls.filter(
      ([arg]) =>
        Array.isArray((arg as { queryKey?: unknown[] })?.queryKey) &&
        (arg as { queryKey: unknown[] }).queryKey[0] === "user",
    );
    expect(conversationInvalidations).toHaveLength(0);
  });
});