mirror of
https://github.com/moltbot/moltbot.git
synced 2026-05-13 15:47:28 +00:00
test: guard google realtime mock calls
This commit is contained in:
@@ -65,6 +65,26 @@ function sentAudio(index = 0): { data?: unknown; mimeType?: unknown } {
|
||||
return audio as { data?: unknown; mimeType?: unknown };
|
||||
}
|
||||
|
||||
function requireFirstMockArg(mock: ReturnType<typeof vi.fn>, label: string): unknown {
|
||||
const [call] = mock.mock.calls;
|
||||
if (!call) {
|
||||
throw new Error(`expected ${label}`);
|
||||
}
|
||||
return call[0];
|
||||
}
|
||||
|
||||
function requireFirstError(mock: ReturnType<typeof vi.fn>): { message?: string } {
|
||||
const error = requireFirstMockArg(mock, "Google Live error");
|
||||
if (!error || typeof error !== "object" || Array.isArray(error)) {
|
||||
throw new Error("expected Google Live error");
|
||||
}
|
||||
return error as { message?: string };
|
||||
}
|
||||
|
||||
function requireFirstAudio(mock: ReturnType<typeof vi.fn>): unknown {
|
||||
return requireFirstMockArg(mock, "Google Live audio");
|
||||
}
|
||||
|
||||
describe("buildGoogleRealtimeVoiceProvider", () => {
|
||||
beforeEach(() => {
|
||||
envSnapshot = Object.fromEntries(ENV_KEYS.map((key) => [key, process.env[key]]));
|
||||
@@ -320,7 +340,7 @@ describe("buildGoogleRealtimeVoiceProvider", () => {
|
||||
});
|
||||
|
||||
expect(createTokenMock).toHaveBeenCalledTimes(1);
|
||||
const tokenConfig = createTokenMock.mock.calls[0]?.[0] as {
|
||||
const tokenConfig = requireFirstMockArg(createTokenMock, "Google Live auth token config") as {
|
||||
config?: {
|
||||
liveConnectConstraints?: {
|
||||
config?: {
|
||||
@@ -445,7 +465,7 @@ describe("buildGoogleRealtimeVoiceProvider", () => {
|
||||
});
|
||||
|
||||
expect(onClose).not.toHaveBeenCalled();
|
||||
const error = onError.mock.calls[0]?.[0] as { message?: string };
|
||||
const error = requireFirstError(onError);
|
||||
expect(error.message).toContain("reconnecting 1/3");
|
||||
|
||||
await vi.advanceTimersByTimeAsync(250);
|
||||
@@ -627,8 +647,9 @@ describe("buildGoogleRealtimeVoiceProvider", () => {
|
||||
});
|
||||
|
||||
expect(onAudio).toHaveBeenCalledTimes(1);
|
||||
expect(onAudio.mock.calls[0]?.[0]).toBeInstanceOf(Buffer);
|
||||
expect(onAudio.mock.calls[0]?.[0]).toHaveLength(80);
|
||||
const audio = requireFirstAudio(onAudio);
|
||||
expect(audio).toBeInstanceOf(Buffer);
|
||||
expect(audio).toHaveLength(80);
|
||||
});
|
||||
|
||||
it("can keep Google PCM output as PCM16 24 kHz audio", async () => {
|
||||
@@ -660,7 +681,7 @@ describe("buildGoogleRealtimeVoiceProvider", () => {
|
||||
});
|
||||
|
||||
expect(onAudio).toHaveBeenCalledTimes(1);
|
||||
expect(onAudio.mock.calls[0]?.[0]).toEqual(pcm24k);
|
||||
expect(requireFirstAudio(onAudio)).toEqual(pcm24k);
|
||||
});
|
||||
|
||||
it("does not forward Google thought text as assistant transcript", async () => {
|
||||
@@ -788,7 +809,7 @@ describe("buildGoogleRealtimeVoiceProvider", () => {
|
||||
bridge.submitToolResult("missing-call", { result: "ok" });
|
||||
|
||||
expect(session.sendToolResponse).not.toHaveBeenCalled();
|
||||
const error = onError.mock.calls[0]?.[0] as { message?: string };
|
||||
const error = requireFirstError(onError);
|
||||
expect(error.message).toBe(
|
||||
"Google Live function response is missing a matching function call for missing-call",
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user