refactor(test): share google-shared test helpers

This commit is contained in:
Peter Steinberger
2026-02-15 14:57:15 +00:00
parent bd9d35c720
commit d3d82a1c19
3 changed files with 53 additions and 79 deletions

View File

@@ -1,41 +1,7 @@
import type { Context, Model } from "@mariozechner/pi-ai/dist/types.js";
import type { Context } from "@mariozechner/pi-ai/dist/types.js";
import { convertMessages } from "@mariozechner/pi-ai/dist/providers/google-shared.js";
import { describe, expect, it } from "vitest";
const asRecord = (value: unknown): Record<string, unknown> => {
expect(value).toBeTruthy();
expect(typeof value).toBe("object");
expect(Array.isArray(value)).toBe(false);
return value as Record<string, unknown>;
};
const makeModel = (id: string): Model<"google-generative-ai"> =>
({
id,
name: id,
api: "google-generative-ai",
provider: "google",
baseUrl: "https://example.invalid",
reasoning: false,
input: ["text"],
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
contextWindow: 1,
maxTokens: 1,
}) as Model<"google-generative-ai">;
const makeGeminiCliModel = (id: string): Model<"google-gemini-cli"> =>
({
id,
name: id,
api: "google-gemini-cli",
provider: "google-gemini-cli",
baseUrl: "https://example.invalid",
reasoning: false,
input: ["text"],
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
contextWindow: 1,
maxTokens: 1,
}) as Model<"google-gemini-cli">;
import { asRecord, makeGeminiCliModel, makeModel } from "./google-shared.test-helpers.js";
describe("google-shared convertTools", () => {
it("ensures function call comes after user turn, not after model turn", () => {

View File

@@ -1,48 +1,7 @@
import type { Context, Model, Tool } from "@mariozechner/pi-ai/dist/types.js";
import type { Context, Tool } from "@mariozechner/pi-ai/dist/types.js";
import { convertMessages, convertTools } from "@mariozechner/pi-ai/dist/providers/google-shared.js";
import { describe, expect, it } from "vitest";
const asRecord = (value: unknown): Record<string, unknown> => {
expect(value).toBeTruthy();
expect(typeof value).toBe("object");
expect(Array.isArray(value)).toBe(false);
return value as Record<string, unknown>;
};
const getFirstToolParameters = (
converted: ReturnType<typeof convertTools>,
): Record<string, unknown> => {
const functionDeclaration = asRecord(converted?.[0]?.functionDeclarations?.[0]);
return asRecord(functionDeclaration.parametersJsonSchema ?? functionDeclaration.parameters);
};
const makeModel = (id: string): Model<"google-generative-ai"> =>
({
id,
name: id,
api: "google-generative-ai",
provider: "google",
baseUrl: "https://example.invalid",
reasoning: false,
input: ["text"],
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
contextWindow: 1,
maxTokens: 1,
}) as Model<"google-generative-ai">;
const _makeGeminiCliModel = (id: string): Model<"google-gemini-cli"> =>
({
id,
name: id,
api: "google-gemini-cli",
provider: "google-gemini-cli",
baseUrl: "https://example.invalid",
reasoning: false,
input: ["text"],
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
contextWindow: 1,
maxTokens: 1,
}) as Model<"google-gemini-cli">;
import { asRecord, getFirstToolParameters, makeModel } from "./google-shared.test-helpers.js";
describe("google-shared convertTools", () => {
it("preserves parameters when type is missing", () => {

View File

@@ -0,0 +1,49 @@
import type { Model } from "@mariozechner/pi-ai/dist/types.js";
import { expect } from "vitest";
export const asRecord = (value: unknown): Record<string, unknown> => {
expect(value).toBeTruthy();
expect(typeof value).toBe("object");
expect(Array.isArray(value)).toBe(false);
return value as Record<string, unknown>;
};
type ConvertedTools = ReadonlyArray<{
functionDeclarations?: ReadonlyArray<{
parametersJsonSchema?: unknown;
parameters?: unknown;
}>;
}>;
export const getFirstToolParameters = (converted: ConvertedTools): Record<string, unknown> => {
const functionDeclaration = asRecord(converted?.[0]?.functionDeclarations?.[0]);
return asRecord(functionDeclaration.parametersJsonSchema ?? functionDeclaration.parameters);
};
export const makeModel = (id: string): Model<"google-generative-ai"> =>
({
id,
name: id,
api: "google-generative-ai",
provider: "google",
baseUrl: "https://example.invalid",
reasoning: false,
input: ["text"],
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
contextWindow: 1,
maxTokens: 1,
}) as Model<"google-generative-ai">;
export const makeGeminiCliModel = (id: string): Model<"google-gemini-cli"> =>
({
id,
name: id,
api: "google-gemini-cli",
provider: "google-gemini-cli",
baseUrl: "https://example.invalid",
reasoning: false,
input: ["text"],
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
contextWindow: 1,
maxTokens: 1,
}) as Model<"google-gemini-cli">;