71 lines
2.4 KiB
TypeScript
71 lines
2.4 KiB
TypeScript
import { act, renderHook } from "@testing-library/react";
|
|
import type { AppendMessage } from "@assistant-ui/react";
|
|
import { describe, expect, it, vi } from "vitest";
|
|
import { extractMessageInput, useHarnessRuntime } from "../src/assistant/use-harness-runtime.js";
|
|
import { createInitialHarnessState, type HarnessClientState } from "../src/store/reducer.js";
|
|
|
|
function appendMessage(text: string): AppendMessage {
|
|
return {
|
|
role: "user",
|
|
content: [{ type: "text", text }],
|
|
attachments: [],
|
|
createdAt: new Date("2026-09-07T08:00:00.000Z"),
|
|
metadata: { custom: {} },
|
|
parentId: null,
|
|
sourceId: null,
|
|
runConfig: undefined,
|
|
};
|
|
}
|
|
|
|
describe("useHarnessRuntime", () => {
|
|
it("turns a composer message into one harness API call", async () => {
|
|
const sendMessage = vi.fn().mockResolvedValue({ runId: "run_1" });
|
|
const cancel = vi.fn().mockResolvedValue({ cancelled: true });
|
|
let snapshot: HarnessClientState = createInitialHarnessState();
|
|
const subscribers = new Set<() => void>();
|
|
const store = {
|
|
getSnapshot: () => snapshot,
|
|
subscribe: (subscriber: () => void) => {
|
|
subscribers.add(subscriber);
|
|
return () => subscribers.delete(subscriber);
|
|
},
|
|
sendMessage,
|
|
cancel,
|
|
};
|
|
|
|
const { result } = renderHook(() => useHarnessRuntime(store));
|
|
await act(async () => {
|
|
result.current.thread.append(appendMessage(" hello agent "));
|
|
await Promise.resolve();
|
|
});
|
|
|
|
expect(sendMessage).toHaveBeenCalledTimes(1);
|
|
expect(sendMessage).toHaveBeenCalledWith({ text: "hello agent", attachmentIds: [] });
|
|
|
|
snapshot = { ...snapshot, runs: { run_1: { runId: "run_1", modelId: "test", status: "running" } } };
|
|
act(() => subscribers.forEach((subscriber) => subscriber()));
|
|
await act(async () => {
|
|
result.current.thread.cancelRun();
|
|
await Promise.resolve();
|
|
});
|
|
expect(cancel).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("extracts uploaded attachment ids without retaining file data", () => {
|
|
const message = appendMessage("caption");
|
|
const input = extractMessageInput({
|
|
...message,
|
|
attachments: [{
|
|
id: "att_one",
|
|
type: "file",
|
|
name: "one.png",
|
|
contentType: "image/png",
|
|
status: { type: "complete" },
|
|
content: [],
|
|
}],
|
|
});
|
|
|
|
expect(input).toEqual({ text: "caption", attachmentIds: ["att_one"] });
|
|
});
|
|
});
|