| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711 |
- import {
- requireTuiAgentAccess,
- requireTuiAccountLogin,
- } from "../../src/application/login-gate.js";
- import type { CliService } from "@mavis/local-runtime-v2/cli-service";
- import { describe, expect, it, vi } from "vitest";
- import { TuiRuntimeAdapter } from "../../src/runtime/adapter.js";
- import { buildTuiSkillCommands } from "../../src/tui/controller/run/active-run-flow.js";
- describe("TuiRuntimeAdapter product access", () => {
- it("forwards the shared Provider Presets and Codex OAuth capability", async () => {
- const listProviderPresets = vi.fn(async () => [
- {
- providerId: "openai",
- name: "OpenAI",
- baseUrl: "https://api.openai.com/v1",
- apiFormat: "openai-responses",
- models: [{ modelId: "gpt-5.6", toolCall: true }],
- },
- ]);
- const getCodexOAuthStatus = vi.fn(async () => ({
- state: "disconnected",
- providerId: "openai-codex",
- }));
- const startCodexOAuthLogin = vi.fn(async () => ({
- state: "pending",
- providerId: "openai-codex",
- authUrl: "https://auth.example",
- }));
- const cancelCodexOAuthLogin = vi.fn(async () => ({
- state: "disconnected",
- providerId: "openai-codex",
- }));
- const adapter = new TuiRuntimeAdapter({
- cancelCodexOAuthLogin,
- listProviderPresets,
- getCodexOAuthStatus,
- startCodexOAuthLogin,
- } as never);
- await expect(adapter.listProviderPresets()).resolves.toEqual([
- expect.objectContaining({
- providerId: "openai",
- apiFormat: "openai-responses",
- }),
- ]);
- await expect(adapter.getCodexOAuthStatus()).resolves.toMatchObject({
- state: "disconnected",
- });
- await expect(
- adapter.startCodexOAuthLogin({ method: "device_code" }),
- ).resolves.toMatchObject({
- state: "pending",
- authUrl: "https://auth.example",
- });
- expect(listProviderPresets).toHaveBeenCalledOnce();
- expect(getCodexOAuthStatus).toHaveBeenCalledOnce();
- expect(startCodexOAuthLogin).toHaveBeenCalledWith({
- method: "device_code",
- });
- await adapter.cancelCodexOAuthLogin("attempt-1");
- expect(cancelCodexOAuthLogin).toHaveBeenCalledWith("attempt-1");
- });
- it("uses the Runtime startup model projection without saving a global selection", async () => {
- const service = {
- listModels: vi.fn(async () => [
- { providerId: "minimax", modelId: "MiniMax-M3", selected: true },
- ]),
- selectModel: vi.fn(async () => true),
- };
- const adapter = new TuiRuntimeAdapter(service as never);
- await expect(adapter.listModels()).resolves.toEqual([
- { providerId: "minimax", modelId: "MiniMax-M3", selected: true },
- ]);
- expect(service.selectModel).not.toHaveBeenCalled();
- });
- it("keeps manual model selection available if no official legacy replacement exists", async () => {
- const work = {
- providerId: "custom_provider:work",
- modelId: "work-model",
- selected: false,
- };
- const selectModel = vi.fn(async () => true);
- const adapter = new TuiRuntimeAdapter({
- listModels: vi.fn(async () => [work]),
- selectModel,
- } as never);
- await expect(adapter.listModels()).resolves.toEqual([work]);
- expect(selectModel).not.toHaveBeenCalled();
- });
- it.each([
- "custom_provider:minimax-legacy",
- "custom_provider:minimax-legacy-2",
- ])(
- "uses the Runtime projection while preserving the stored Session alias: %s",
- async (providerId) => {
- const legacy = { providerId, modelId: "MiniMax-M3" };
- const official = { providerId: "minimax", modelId: "MiniMax-M3" };
- const service = {
- listModels: vi.fn(async () => [
- { ...official, selected: true },
- {
- providerId: "custom_provider:work",
- modelId: "work-model",
- selected: false,
- },
- ]),
- getSession: vi.fn(async () => ({ session: { model: legacy } })),
- getMessages: vi.fn(async () => ({ messages: [] })),
- selectModel: vi.fn(async () => true),
- };
- const adapter = new TuiRuntimeAdapter(service as never);
- const models = await adapter.listModels("session-1");
- expect(service.selectModel).not.toHaveBeenCalled();
- expect(models.find((model) => model.selected)).toEqual({
- ...official,
- selected: true,
- });
- expect(models.map((model) => model.providerId)).toEqual([
- "minimax",
- "custom_provider:work",
- ]);
- await expect(
- adapter.getContextSnapshot("session-1"),
- ).resolves.toMatchObject({
- model: { provider: "minimax", id: "MiniMax-M3" },
- });
- },
- );
- it("forwards tested provider candidates through the process-local Runtime", async () => {
- const saveUserModelProviderCandidate = vi.fn(async () => ({
- success: true,
- provider: { providerId: "custom_provider:deepseek" },
- }));
- const adapter = new TuiRuntimeAdapter({
- saveUserModelProviderCandidate,
- } as never);
- await expect(
- adapter.saveUserModelProviderCandidate({
- name: "DeepSeek",
- baseUrl: "https://api.deepseek.com/v1",
- apiKey: "sk-test",
- apiFormat: "openai-completions",
- models: [{ modelId: "deepseek-chat", toolCall: true }],
- modelId: "deepseek-chat",
- saveAndUse: true,
- }),
- ).resolves.toMatchObject({ success: true });
- expect(saveUserModelProviderCandidate).toHaveBeenCalledWith({
- candidate: {
- name: "DeepSeek",
- baseUrl: "https://api.deepseek.com/v1",
- apiKey: "sk-test",
- apiFormat: "openai-completions",
- models: [{ modelId: "deepseek-chat", toolCall: true }],
- },
- modelId: "deepseek-chat",
- saveAndUse: true,
- });
- });
- it("projects the process-local model catalog with the same thinking shape as Desktop", async () => {
- const listModels = vi.fn(async () => [
- {
- providerId: "custom_provider:openai",
- modelId: "gpt-5.6",
- thinkingConfig: { mode: "switchable", default_value: "true" },
- effortOptions: ["low", "high", "max"],
- },
- ]);
- const getSession = vi.fn(async () => ({ session: { id: "session-1" } }));
- const adapter = new TuiRuntimeAdapter({ listModels, getSession } as never);
- await expect(adapter.listModels("session-1")).resolves.toEqual([
- {
- providerId: "custom_provider:openai",
- modelId: "gpt-5.6",
- thinkingConfig: { mode: "switchable", defaultValue: "true" },
- effortOptions: ["low", "high", "max"],
- },
- ]);
- expect(listModels).toHaveBeenCalledWith({ sessionId: "session-1" });
- expect(getSession).not.toHaveBeenCalled();
- });
- it("reuses generated CliService usage, context, compaction, and Session contracts", async () => {
- const cliService = cliServiceFixture();
- const adapter = new TuiRuntimeAdapter(cliService);
- await expect(adapter.getSessionUsage("session-1")).resolves.toEqual({
- summary: { inputTokens: 10, outputTokens: 5, totalTokens: 15, turns: 1 },
- rows: [{ model: "minimax/MiniMax-M3" }],
- });
- await expect(adapter.getSessionUsageSummary("session-1")).resolves.toEqual({
- inputTokens: 10,
- cacheReadTokens: 80,
- cacheWriteTokens: 10,
- });
- expect(cliService.getSessionUsageSummary).toHaveBeenCalledWith({
- id: "session-1",
- });
- const signal = new AbortController().signal;
- const commits = adapter.watchSessionUsageCommits(signal);
- await expect(commits.next()).resolves.toEqual({
- done: false,
- value: "session-1",
- });
- expect(cliService.watchSessionUsageCommits).toHaveBeenCalledWith(signal);
- await expect(
- adapter.requestCompaction("session-1", "mavis", "keep decisions"),
- ).resolves.toMatchObject({
- success: true,
- sessionId: "session-1",
- tokensBefore: 100,
- tokensAfter: 40,
- });
- expect(cliService.requestCompaction).toHaveBeenCalledWith({
- name: "mavis",
- id: "session-1",
- reason: "ui_request",
- customInstructions: "keep decisions",
- });
- await expect(
- adapter.getContextSnapshot("session-1"),
- ).resolves.toMatchObject({
- status: "stale",
- model: { provider: "minimax", id: "MiniMax-M3", contextWindow: 1000 },
- contextUsage: {
- contextWindowTokens: 1000,
- usedTokens: 300,
- totalCountSource: "PROVIDER_USAGE_ANCHORED",
- },
- });
- expect(cliService.getMessages).toHaveBeenCalledWith({
- id: "session-1",
- limit: 80,
- });
- expect(cliService.listModels).toHaveBeenCalledWith({
- sessionId: "session-1",
- });
- await expect(adapter.getActiveRun("session-1")).resolves.toEqual({
- schemaVersion: 1,
- sessionId: "session-1",
- state: "running",
- turnId: "turn-1",
- actions: { steer: true },
- });
- expect(cliService.getActiveTurn).toHaveBeenCalledWith("session-1");
- });
- it("derives decision-blocked active state from generated interaction contracts", async () => {
- const cliService = cliServiceFixture();
- vi.mocked(cliService.listPendingPermissions).mockResolvedValueOnce({
- requests: [
- {
- requestId: "permission-1",
- sessionId: "session-1",
- agentName: "mavis",
- toolName: "bash",
- ruleContents: ["git status"],
- reason: "needs approval",
- allowAlwaysSupported: true,
- createdAt: 10,
- },
- ],
- });
- const adapter = new TuiRuntimeAdapter(cliService);
- await expect(adapter.getActiveRun("session-1")).resolves.toMatchObject({
- state: "decision-blocked",
- actions: { steer: false },
- });
- });
- it("does not advertise steer for a Turn owned by another Runtime", async () => {
- const cliService = cliServiceFixture();
- vi.mocked(cliService.getActiveTurn).mockResolvedValueOnce({
- turnId: "turn-foreign",
- busyReason: "turn",
- locallyOwned: false,
- });
- const adapter = new TuiRuntimeAdapter(cliService);
- await expect(adapter.getActiveRun("session-1")).resolves.toMatchObject({
- state: "running",
- turnId: "turn-foreign",
- actions: { steer: false },
- });
- });
- it("routes peripheral product capabilities through CliService", async () => {
- const adapter = new TuiRuntimeAdapter(cliServiceFixture());
- await expect(adapter.getAccountStatus("session-1")).resolves.toMatchObject({
- status: "ready",
- defaultModel: "minimax/MiniMax-M3",
- });
- await expect(adapter.getRuntimeDiagnostics()).resolves.toMatchObject({
- status: "ok",
- });
- await expect(adapter.getInstructionSources("/repo")).resolves.toEqual([
- { scope: "project", path: "/repo/AGENTS.md" },
- ]);
- await expect(adapter.getPermissionMode()).resolves.toBe("default");
- await expect(adapter.setPermissionMode("bypassPermissions")).resolves.toBe(
- "bypassPermissions",
- );
- await expect(adapter.listModels("session-1")).resolves.toEqual([
- {
- providerId: "minimax",
- modelId: "MiniMax-M3",
- selected: true,
- contextLimit: 400_000,
- },
- ]);
- await expect(adapter.listSkills("mavis")).resolves.toEqual({
- skills: [{ name: "review", enabled: true }],
- hasMore: false,
- });
- await expect(adapter.listMcpServers()).resolves.toEqual([
- { name: "browser", enabled: true, transport: "stdio" },
- ]);
- });
- it("projects only verified identity fields during lightweight account hydration", async () => {
- const tokenPlanAccountStatusGetter = vi.fn(async () => ({
- quotaState: "available" as const,
- quota: {
- fiveHour: { remainingPercent: 90, unlimited: false },
- weekly: { remainingPercent: 80, unlimited: false },
- },
- }));
- const service = cliServiceFixture();
- const adapter = new TuiRuntimeAdapter(service, {
- tokenPlanAccountStatusGetter,
- accountIdentityGetter: () => ({
- email: "dev@example.com",
- name: "Dev",
- accessToken: "never-project",
- }),
- });
- await expect(
- adapter.getAccountStatus(undefined, { includeMembership: false }),
- ).resolves.toMatchObject({
- modelSource: "token-plan",
- managedTokenPresent: true,
- });
- expect(tokenPlanAccountStatusGetter).not.toHaveBeenCalled();
- const account = await adapter.getAccountStatus(undefined, {
- includeMembership: false,
- });
- expect(account.identity).toEqual({ email: "dev@example.com", name: "Dev" });
- expect(JSON.stringify(account)).not.toContain("never-project");
- await expect(
- adapter.getAccountStatus(undefined, { includeMembership: true }),
- ).resolves.toMatchObject({
- tokenPlanQuotaState: "available",
- tokenPlanQuota: { fiveHour: { remainingPercent: 90 } },
- });
- expect(tokenPlanAccountStatusGetter).toHaveBeenCalledOnce();
- vi.mocked(service.getAccountStatus).mockResolvedValueOnce({
- auth: { tokenPresent: false },
- });
- expect(
- (await adapter.getAccountStatus(undefined, { includeMembership: false }))
- .identity,
- ).toBeUndefined();
- });
- it("keeps BYOK usable while account-only actions surface unavailable OAuth", async () => {
- const cliService = cliServiceFixture();
- vi.mocked(cliService.getAccountStatus).mockResolvedValue({
- selection: { defaultModel: "custom/model" },
- provider: { id: "custom_provider:example", authMode: "api-key" },
- auth: { tokenPresent: false },
- warnings: [],
- });
- const failure = new TypeError("fetch failed");
- const synchronizeAuth = vi.fn(async () => {
- throw failure;
- });
- const adapter = new TuiRuntimeAdapter(cliService, { synchronizeAuth });
- await expect(requireTuiAgentAccess(adapter)).resolves.toMatchObject({
- modelSource: "byok",
- });
- expect(synchronizeAuth).not.toHaveBeenCalled();
- await expect(requireTuiAccountLogin(adapter)).rejects.toBe(failure);
- expect(synchronizeAuth).toHaveBeenCalledOnce();
- });
- it("keeps BYOK usable while account-only actions surface unavailable OAuth", async () => {
- const cliService = cliServiceFixture();
- vi.mocked(cliService.getAccountStatus).mockResolvedValue({
- selection: { defaultModel: "custom/model" },
- provider: { id: "custom_provider:example", authMode: "api-key" },
- auth: { tokenPresent: false },
- warnings: [],
- });
- const failure = new TypeError("fetch failed");
- const synchronizeAuth = vi.fn(async () => {
- throw failure;
- });
- const adapter = new TuiRuntimeAdapter(cliService, { synchronizeAuth });
- await expect(requireTuiAgentAccess(adapter)).resolves.toMatchObject({
- modelSource: "byok",
- });
- expect(synchronizeAuth).not.toHaveBeenCalled();
- await expect(requireTuiAccountLogin(adapter)).rejects.toBe(failure);
- expect(synchronizeAuth).toHaveBeenCalledOnce();
- });
- it("hydrates explicit Token Plan usage for a BYOK account", async () => {
- const cliService = cliServiceFixture();
- vi.mocked(cliService.getAccountStatus).mockResolvedValueOnce({
- selection: { defaultModel: "custom/gpt-5.6" },
- provider: { id: "custom_provider:openai", authMode: "api-key" },
- auth: { tokenPresent: false },
- warnings: [],
- });
- const tokenPlanAccountStatusGetter = vi.fn(async () => ({
- summary: { tier: "Pro Plan" },
- quotaState: "available" as const,
- quota: {
- fiveHour: { remainingPercent: 75, unlimited: false },
- weekly: { remainingPercent: 50, unlimited: false },
- },
- }));
- const adapter = new TuiRuntimeAdapter(cliService, {
- tokenPlanAccountStatusGetter,
- });
- await expect(
- adapter.getAccountStatus(undefined, {
- includeMembership: true,
- forceRefresh: true,
- }),
- ).resolves.toMatchObject({
- modelSource: "byok",
- managedTokenPresent: false,
- tokenPlanSummary: { tier: "Pro Plan" },
- tokenPlanQuotaState: "available",
- tokenPlanQuota: { fiveHour: { remainingPercent: 75 } },
- });
- expect(tokenPlanAccountStatusGetter).toHaveBeenCalledWith({
- forceRefresh: true,
- });
- });
- it("forwards an explicit account refresh to Token Plan hydration", async () => {
- const tokenPlanAccountStatusGetter = vi.fn(async () => ({
- quotaState: "available" as const,
- quota: {
- fiveHour: { remainingPercent: 90, unlimited: false },
- weekly: { remainingPercent: 80, unlimited: false },
- },
- }));
- const adapter = new TuiRuntimeAdapter(cliServiceFixture(), {
- tokenPlanAccountStatusGetter,
- });
- await adapter.getAccountStatus(undefined, {
- includeMembership: true,
- forceRefresh: true,
- });
- expect(tokenPlanAccountStatusGetter).toHaveBeenCalledWith({
- forceRefresh: true,
- });
- });
- it("updates both the active Session and the TUI default model", async () => {
- const cliService = cliServiceFixture();
- const adapter = new TuiRuntimeAdapter(cliService);
- const selection = {
- providerId: "minimax",
- modelId: "MiniMax-M3",
- variant: "thinking",
- contextLimit: 1_000_000,
- thinking: { effort: "high" },
- };
- const defaultSelection = {
- providerId: selection.providerId,
- modelId: selection.modelId,
- variant: selection.variant,
- contextLimit: selection.contextLimit,
- thinking: selection.thinking,
- };
- await expect(adapter.selectModel(selection, "session-1")).resolves.toBe(
- true,
- );
- expect(cliService.selectModel).toHaveBeenNthCalledWith(1, defaultSelection);
- expect(cliService.selectModel).toHaveBeenNthCalledWith(2, {
- ...selection,
- sessionId: "session-1",
- });
- vi.mocked(cliService.selectModel).mockClear();
- await expect(adapter.selectModel(selection)).resolves.toBe(true);
- expect(cliService.selectModel).toHaveBeenCalledOnce();
- expect(cliService.selectModel).toHaveBeenCalledWith(defaultSelection);
- });
- it("can update one Session model without changing the process default", async () => {
- const cliService = cliServiceFixture();
- const adapter = new TuiRuntimeAdapter(cliService);
- const selection = {
- providerId: "minimax",
- modelId: "MiniMax-M3",
- variant: "thinking",
- thinking: { effort: "high" },
- };
- await expect(
- adapter.selectSessionModel(selection, "session-1"),
- ).resolves.toBe(true);
- expect(cliService.selectModel).toHaveBeenCalledOnce();
- expect(cliService.selectModel).toHaveBeenCalledWith({
- ...selection,
- sessionId: "session-1",
- });
- });
- it("does not change the active Session when saving the TUI default is rejected", async () => {
- const cliService = cliServiceFixture();
- vi.mocked(cliService.selectModel).mockResolvedValueOnce(false);
- const adapter = new TuiRuntimeAdapter(cliService);
- const selection = {
- providerId: "minimax",
- modelId: "MiniMax-M3",
- };
- await expect(adapter.selectModel(selection, "session-1")).resolves.toBe(
- false,
- );
- expect(cliService.selectModel).toHaveBeenCalledOnce();
- expect(cliService.selectModel).toHaveBeenCalledWith(selection);
- });
- it("registers installed Plugin Skills from the unified Runtime roster as slash commands", async () => {
- const cliService = cliServiceFixture();
- vi.mocked(cliService.listRuntimeSkills).mockImplementationOnce(
- async (input) => {
- const skills = [{ name: "review", enabled: true }];
- if (input.includePluginSkills === true) {
- skills.push({ name: "traceink:generate", enabled: true });
- }
- return { skills, refreshedAt: 123 };
- },
- );
- const adapter = new TuiRuntimeAdapter(cliService);
- const result = await adapter.listSkills("mavis");
- expect(cliService.listRuntimeSkills).toHaveBeenCalledWith({
- agentName: "mavis",
- includePluginSkills: true,
- });
- expect(result.skills).toHaveLength(2);
- expect(buildTuiSkillCommands(result)).toEqual(
- expect.arrayContaining([
- expect.objectContaining({ name: "traceink:generate" }),
- ]),
- );
- expect(result.hasMore).toBe(false);
- });
- });
- function cliServiceFixture(): CliService {
- return {
- watchSessionUsageCommits: vi.fn(async function* watchSessionUsageCommits() {
- yield "session-1";
- }),
- getSessionUsageSummary: vi.fn(async () => ({
- inputTokens: 10,
- cacheReadTokens: 80,
- cacheWriteTokens: 10,
- })),
- getSessionUsage: vi.fn(async () => ({
- summary: { inputTokens: 10, outputTokens: 5, totalTokens: 15, turns: 1 },
- rows: [{ model: "minimax/MiniMax-M3" }],
- })),
- getMessages: vi.fn(async () => ({
- messages: [
- {
- msgId: "assistant-1",
- kind: "final",
- rawJson: JSON.stringify({
- msg_id: "assistant-1",
- turnId: "turn-1",
- context_usage: {
- contextWindowTokens: 1000,
- usedTokens: 300,
- totalCountSource: "PROVIDER_USAGE_ANCHORED",
- components: [{ kind: "MESSAGES", tokens: 300 }],
- },
- }),
- },
- ],
- })),
- requestCompaction: vi.fn(async () => ({
- success: true,
- sessionId: "session-1",
- compactionId: "compaction-1",
- messagesBefore: 10,
- messagesAfter: 4,
- tokensBefore: 100,
- tokensAfter: 40,
- })),
- getSession: vi.fn(async () => ({
- session: {
- sessionId: "session-1",
- agentName: "mavis",
- status: { statusType: 1 },
- model: {
- providerId: "minimax",
- modelId: "MiniMax-M3",
- contextLimit: 1000,
- },
- },
- })),
- listModels: vi.fn(async () => [
- {
- providerId: "minimax",
- modelId: "MiniMax-M3",
- selected: true,
- contextLimit: 400_000,
- },
- ]),
- getActiveTurn: vi.fn(async () => ({
- turnId: "turn-1",
- busyReason: "turn" as const,
- locallyOwned: true,
- })),
- listPendingPermissions: vi.fn(async () => ({ requests: [] })),
- getPendingQuestionnaire: vi.fn(async () => ({})),
- getAccountStatus: vi.fn(async () => ({
- selection: { defaultModel: "minimax/MiniMax-M3" },
- provider: { id: "minimax", authMode: "managed-login" },
- auth: { tokenPresent: true },
- warnings: [],
- })),
- getRuntimeDiagnostics: vi.fn(async () => ({ status: "ok" })),
- getInstructionSources: vi.fn(async ({ workspaceDir }) => [
- { scope: "project", path: `${workspaceDir}/AGENTS.md` },
- ]),
- getPermissionMode: vi.fn(async () => "default"),
- setPermissionMode: vi.fn(async ({ mode }) => mode),
- selectModel: vi.fn(async () => true),
- listRuntimeSkills: vi.fn(async () => ({
- skills: [{ name: "review", enabled: true }],
- refreshedAt: 123,
- })),
- listMcpServers: vi.fn(async () => ({
- servers: [{ name: "browser", enabled: true, transport: "stdio" }],
- })),
- } as unknown as CliService;
- }
- it("forwards saved-credential discovery and revision-checked model-only updates", async () => {
- const discoverUserModelsCandidate = vi.fn(async () => [
- { modelId: "latest" },
- ]);
- const saveUserModelProviderCandidate = vi.fn(async () => ({ success: true }));
- const adapter = new TuiRuntimeAdapter({
- discoverUserModelsCandidate,
- saveUserModelProviderCandidate,
- } as never);
- const candidate = {
- providerId: "custom_provider:work",
- expectedRevision: "rev-1",
- baseUrl: "https://models.example/v1",
- };
- await expect(adapter.discoverUserModelsCandidate(candidate)).resolves.toEqual(
- [{ modelId: "latest" }],
- );
- expect(discoverUserModelsCandidate).toHaveBeenCalledWith(candidate);
- await adapter.saveUserModelProviderCandidate({
- ...candidate,
- models: [{ modelId: "latest" }],
- modelId: "latest",
- skipConnectionTest: true,
- saveAndUse: false,
- });
- expect(saveUserModelProviderCandidate).toHaveBeenCalledWith({
- candidate: { ...candidate, models: [{ modelId: "latest" }] },
- modelId: "latest",
- skipConnectionTest: true,
- saveAndUse: false,
- });
- await adapter.saveUserModelProviderCandidate({
- ...candidate,
- apiKey: "new-key",
- modelId: "latest",
- saveAndUse: false,
- });
- expect(saveUserModelProviderCandidate).toHaveBeenLastCalledWith({
- candidate: { ...candidate, apiKey: "new-key" },
- modelId: "latest",
- saveAndUse: false,
- });
- });
|