File size: 1,723 Bytes
4e7644b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
53
54
55
56
/**
 * Tests for api/modules/env.ts
 *
 * Contract-guard style: verify return pass-through.  `saveEnvs` is a
 * full-replacement batch op; `deleteEnv` removes one key.  We verify
 * shape and error propagation, not exact request body strings (#5438).
 */

import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";

vi.mock("../request", () => ({
  request: vi.fn(),
}));

import { envApi } from "./env";
import { request } from "../request";

describe("envApi", () => {
  beforeEach(() => {
    vi.mocked(request).mockReset();
  });

  afterEach(() => {
    vi.clearAllMocks();
  });

  it("listEnvs returns the EnvVar[] from request", async () => {
    const envs = [
      { key: "API_KEY", value: "v1", is_secret: true },
      { key: "DEBUG", value: "false", is_secret: false },
    ];
    vi.mocked(request).mockResolvedValue(envs);
    const result = await envApi.listEnvs();
    expect(result).toEqual(envs);
  });

  it("saveEnvs returns the full updated EnvVar[] after batch save", async () => {
    const updated = [{ key: "K", value: "V", is_secret: false }];
    vi.mocked(request).mockResolvedValue(updated);
    const result = await envApi.saveEnvs({ K: "V" });
    expect(result).toBe(updated);
  });

  it("deleteEnv returns the remaining EnvVar[] after deletion", async () => {
    const remaining = [{ key: "OTHER", value: "x", is_secret: false }];
    vi.mocked(request).mockResolvedValue(remaining);
    const result = await envApi.deleteEnv("OLD_KEY");
    expect(result).toBe(remaining);
  });

  it("propagates request errors", async () => {
    vi.mocked(request).mockRejectedValue(new Error("conflict"));
    await expect(envApi.saveEnvs({})).rejects.toThrow("conflict");
  });
});