Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

3.1 KB
Raw Blame
import { test } from "node:test";
import assert from "node:assert/strict";
import { mkdtempSync, rmSync, statSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import {
  buildKiloAuth,
  buildKiloVscodeSettings,
  resolveKiloTarget,
  runSetupKiloCommand,
} from "../../../bin/cli/commands/setup-kilo.mjs";

test("buildKiloAuth sets the openai-compatible provider (baseUrl WITH /v1, model)", () => {
  const auth = buildKiloAuth(
    {},
    { apiKey: "sk-x", baseUrl: "http://vps:20128/v1", model: "glm/glm-5.2" }
  );
  assert.equal(auth["openai-compatible"].apiKey, "sk-x");
  assert.equal(auth["openai-compatible"].baseUrl, "http://vps:20128/v1");
  assert.equal(auth["openai-compatible"].model, "glm/glm-5.2");
});

test("buildKiloAuth merges (preserves other providers/keys)", () => {
  const auth = buildKiloAuth(
    { anthropic: { apiKey: "keep" } },
    { apiKey: "k", baseUrl: "http://x/v1", model: "m" }
  );
  assert.equal(auth.anthropic.apiKey, "keep");
  assert.equal(auth["openai-compatible"].model, "m");
});

test("buildKiloAuth falls back to a placeholder key", () => {
  const auth = buildKiloAuth({}, { apiKey: "", baseUrl: "http://x/v1", model: "m" });
  assert.equal(auth["openai-compatible"].apiKey, "sk_omniroute");
});

test("buildKiloVscodeSettings sets kilocode.customProvider + defaultModel, preserving others", () => {
  const s = buildKiloVscodeSettings(
    { "editor.fontSize": 14 },
    { apiKey: "k", baseUrl: "http://vps:20128/v1", model: "glm/glm-5.2" }
  );
  assert.equal(s["editor.fontSize"], 14);
  assert.equal(s["kilocode.customProvider"].name, "OmniRoute");
  assert.equal(s["kilocode.customProvider"].baseURL, "http://vps:20128/v1");
  assert.equal(s["kilocode.defaultModel"], "glm/glm-5.2");
});

test("resolveKiloTarget ensures /v1 on the base URL (Kilo wants it)", () => {
  assert.equal(resolveKiloTarget({ remote: "http://vps:20128" }).baseUrl, "http://vps:20128/v1");
  assert.equal(
    resolveKiloTarget({ remote: "http://vps:20128/v1/" }).baseUrl,
    "http://vps:20128/v1"
  );
});

test("resolveKiloTarget: explicit --api-key wins", () => {
  assert.equal(
    resolveKiloTarget({ remote: "http://x:20128", apiKey: "sk-explicit" }).apiKey,
    "sk-explicit"
  );
});

test("setup-kilo writes CLI and VS Code credential files with private permissions", async () => {
  const directory = mkdtempSync(join(tmpdir(), "omniroute-kilo-private-"));
  const authPath = join(directory, "kilo", "auth.json");
  const vscodePath = join(directory, "settings.json");
  writeFileSync(vscodePath, JSON.stringify({ "editor.fontSize": 14 }));

  try {
    assert.equal(
      await runSetupKiloCommand({
        authPath,
        vscodeSettings: vscodePath,
        remote: "http://127.0.0.1:20128",
        apiKey: "provider-secret",
        model: "glm/glm-5.2",
        yes: true,
        allowContainerWrite: true,
      }),
      0
    );
    assert.equal(statSync(authPath).mode & 0o777, 0o600);
    assert.equal(statSync(vscodePath).mode & 0o777, 0o600);
  } finally {
    rmSync(directory, { recursive: true, force: true });
  }
});