/ opencodehub / tests / unit / ai-config.test.ts
swadhinbiswas feat: OpenCodeHub v1.1.0 - Enterprise Features & Storage Adapters · 2/15/2026
719a1cb
52 lines · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { describe, expect, it } from "vitest";
import {
  buildStoredAIConfig,
  parseAIConfigFromStorage,
  sanitizeAIConfigForClient,
} from "@/lib/ai-config";

describe("ai-config security", () => {
  it("encrypts keys at rest and decrypts on read", () => {
    process.env.AI_CONFIG_ENCRYPTION_KEY = "test-encryption-key-for-unit-tests";

    const stored = buildStoredAIConfig({
      provider: "openai",
      model: "gpt-4-turbo",
      apiKeys: {
        openai: "sk-test-super-secret",
      },
    });

    expect(stored).not.toContain("sk-test-super-secret");

    const parsed = parseAIConfigFromStorage(stored);
    expect(parsed.apiKeys.openai).toBe("sk-test-super-secret");
  });

  it("supports legacy plaintext values for backward compatibility", () => {
    const legacy = JSON.stringify({
      provider: "openai",
      model: "gpt-4-turbo",
      apiKeys: { openai: "sk-legacy-plain" },
    });

    const parsed = parseAIConfigFromStorage(legacy);
    expect(parsed.apiKeys.openai).toBe("sk-legacy-plain");
  });

  it("sanitizes config for client responses", () => {
    const redacted = sanitizeAIConfigForClient({
      provider: "openai",
      model: "gpt-4-turbo",
      apiKeys: {
        openai: "sk-test-super-secret",
      },
    });

    expect(redacted.apiKeys.openai.isSet).toBe(true);
    expect(redacted.apiKeys.openai.masked).toBe("••••cret");
    expect(redacted.apiKeys.groq.isSet).toBe(false);
  });
});