mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-19 11:08:37 +00:00
refactor(test): share google-shared test helpers
This commit is contained in:
@@ -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 { convertMessages } from "@mariozechner/pi-ai/dist/providers/google-shared.js";
|
||||||
import { describe, expect, it } from "vitest";
|
import { describe, expect, it } from "vitest";
|
||||||
|
import { asRecord, makeGeminiCliModel, makeModel } from "./google-shared.test-helpers.js";
|
||||||
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">;
|
|
||||||
|
|
||||||
describe("google-shared convertTools", () => {
|
describe("google-shared convertTools", () => {
|
||||||
it("ensures function call comes after user turn, not after model turn", () => {
|
it("ensures function call comes after user turn, not after model turn", () => {
|
||||||
|
|||||||
@@ -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 { convertMessages, convertTools } from "@mariozechner/pi-ai/dist/providers/google-shared.js";
|
||||||
import { describe, expect, it } from "vitest";
|
import { describe, expect, it } from "vitest";
|
||||||
|
import { asRecord, getFirstToolParameters, makeModel } from "./google-shared.test-helpers.js";
|
||||||
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">;
|
|
||||||
|
|
||||||
describe("google-shared convertTools", () => {
|
describe("google-shared convertTools", () => {
|
||||||
it("preserves parameters when type is missing", () => {
|
it("preserves parameters when type is missing", () => {
|
||||||
|
|||||||
49
src/providers/google-shared.test-helpers.ts
Normal file
49
src/providers/google-shared.test-helpers.ts
Normal 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">;
|
||||||
Reference in New Issue
Block a user