File size: 11,999 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/**
 * Tests for api/modules/backup.ts
 *
 * Design principle: test **behaviour**, not transport.
 * We verify return values, error shapes, and state transitions β€” not the
 * exact URL, HTTP method, or request body that the implementation happens
 * to send.  This keeps the tests resilient when the API client internals
 * change (e.g. switching from `request()` to direct `fetch`, renaming query
 * params, or adjusting Content-Type headers).
 *
 * Only functions with non-trivial control-flow (SSE streaming, conflict
 * handling, cancel-suppression) get dedicated tests.  Thin wrappers that
 * simply forward to `request()` or `downloadFileFromUrl()` are not tested
 * here β€” their contract is already covered by the shared `request` module.
 */

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

// ---------------------------------------------------------------------------
// Module stubs β€” keep them minimal; only provide what the code-under-test
// actually needs so we don't over-couple to irrelevant internals.
// ---------------------------------------------------------------------------
vi.mock("../request", () => ({
  request: vi.fn(),
}));
vi.mock("../config", () => ({
  getApiUrl: vi.fn((p: string) => `http://test${p}`),
}));
vi.mock("../authHeaders", () => ({
  buildAuthHeaders: vi.fn(() => ({})),
}));
vi.mock("../../utils/downloadFileFromUrl", () => {
  class DownloadCancelledError extends Error {
    constructor() {
      super("Download cancelled");
      this.name = "DownloadCancelledError";
    }
  }
  return {
    DownloadCancelledError,
    downloadFileFromUrl: vi.fn(),
  };
});

import { backupApi } from "./backup";
import {
  downloadFileFromUrl,
  DownloadCancelledError,
} from "../../utils/downloadFileFromUrl";

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

interface MockResponseOptions {
  ok?: boolean;
  status: number;
  json?: unknown;
  text?: string;
  body?: ReadableStream<Uint8Array>;
}

function mockResponse({
  ok,
  status,
  json,
  text,
  body,
}: MockResponseOptions): Response {
  return {
    ok: ok ?? (status >= 200 && status < 300),
    status,
    json: async () => json,
    text: async () => text ?? "",
    body,
  } as unknown as Response;
}

/** Build a fake Response whose body yields the given SSE chunks. */
function makeSseResponse(chunks: string[]): Response {
  const encoder = new TextEncoder();
  const stream = new ReadableStream<Uint8Array>({
    start(controller) {
      for (const c of chunks) controller.enqueue(encoder.encode(c));
      controller.close();
    },
  });
  return mockResponse({ ok: true, status: 200, body: stream });
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

describe("backupApi", () => {
  beforeEach(() => {
    vi.mocked(downloadFileFromUrl).mockReset();
    vi.mocked(downloadFileFromUrl).mockResolvedValue(undefined);
  });

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

  // =========================================================================
  // createBackupStream β€” SSE parsing + event-driven progress
  // =========================================================================
  // This is the most complex function: it manually reads an SSE stream,
  // parses `data:` lines, calls onEvent for each, and returns the `meta`
  // from the "done" event.  Several branches need guarding:
  //   - normal happy path (events β†’ done event β†’ return meta)
  //   - error event β†’ throws with the event's message
  //   - stream ends without a done event β†’ throws
  // =========================================================================

  describe("createBackupStream", () => {
    it("calls onEvent for each SSE event and returns the done-event meta", async () => {
      const agentEvent = {
        type: "agent",
        agent_id: "a1",
        index: 0,
        total: 1,
        percent: 50,
      } as const;
      const doneEvent = {
        type: "done",
        meta: { id: "b1", name: "n" },
        percent: 100,
      } as const;
      global.fetch = vi
        .fn()
        .mockResolvedValue(
          makeSseResponse([
            `data: ${JSON.stringify(agentEvent)}\n\n`,
            `data: ${JSON.stringify(doneEvent)}\n\n`,
          ]),
        );

      const onEvent = vi.fn();
      const meta = await backupApi.createBackupStream(
        {
          name: "n",
          scope: {
            include_agents: true,
            include_global_config: false,
            include_secrets: false,
            include_skill_pool: false,
          },
          agents: ["a1"],
        },
        onEvent,
      );

      // The onEvent callback must be invoked once per SSE event
      expect(onEvent).toHaveBeenCalledTimes(2);
      expect(onEvent).toHaveBeenNthCalledWith(1, agentEvent);
      expect(onEvent).toHaveBeenNthCalledWith(2, doneEvent);
      // The returned meta must come from the "done" event
      expect(meta).toEqual({ id: "b1", name: "n" });
    });

    it("throws the error-event message when the server sends an error event", async () => {
      const errorEvent = { type: "error", message: "disk full" };
      global.fetch = vi
        .fn()
        .mockResolvedValue(
          makeSseResponse([`data: ${JSON.stringify(errorEvent)}\n\n`]),
        );

      await expect(
        backupApi.createBackupStream(
          {
            name: "n",
            scope: {
              include_agents: false,
              include_global_config: false,
              include_secrets: false,
              include_skill_pool: false,
            },
            agents: [],
          },
          () => {},
        ),
      ).rejects.toThrow("disk full");
    });

    it("throws when the stream ends without ever sending a done event", async () => {
      // An agent event arrives but the server drops the connection before "done"
      const agentEvent = {
        type: "agent",
        agent_id: "a1",
        index: 0,
        total: 1,
        percent: 50,
      };
      global.fetch = vi
        .fn()
        .mockResolvedValue(
          makeSseResponse([`data: ${JSON.stringify(agentEvent)}\n\n`]),
        );

      await expect(
        backupApi.createBackupStream(
          {
            name: "n",
            scope: {
              include_agents: false,
              include_global_config: false,
              include_secrets: false,
              include_skill_pool: false,
            },
            agents: [],
          },
          () => {},
        ),
      ).rejects.toThrow("No completion event received");
    });

    it("ignores non-data SSE lines (skips chunks without 'data: ' prefix)", async () => {
      const doneEvent = {
        type: "done",
        meta: { id: "b1", name: "n" },
        percent: 100,
      };
      global.fetch = vi.fn().mockResolvedValue(
        makeSseResponse([
          `: this is a comment\n\n`, // SSE comment β€” should be ignored
          `event: agent\n\n`, // event field without data β€” ignored
          `data: ${JSON.stringify(doneEvent)}\n\n`,
        ]),
      );

      const onEvent = vi.fn();
      const meta = await backupApi.createBackupStream(
        {
          name: "n",
          scope: {
            include_agents: false,
            include_global_config: false,
            include_secrets: false,
            include_skill_pool: false,
          },
          agents: [],
        },
        onEvent,
      );

      // Only the data-prefixed line should trigger onEvent
      expect(onEvent).toHaveBeenCalledTimes(1);
      expect(meta).toEqual({ id: "b1", name: "n" });
    });
  });

  // =========================================================================
  // importBackup β€” conflict handling (HTTP 409)
  // =========================================================================
  // This is the one error path in the module that has **structured data**
  // attached to the thrown error.  If the 409 branch breaks, the UI cannot
  // present the conflict-resolution dialog, so this is a high-value guard.
  // =========================================================================

  describe("importBackup", () => {
    it("on HTTP 409, throws Error('backup_conflict') with .conflict carrying the server body", async () => {
      const conflictBody = {
        detail: "backup_conflict" as const,
        existing: { id: "b0", name: "old" },
        pending_token: "tok-123",
      };
      global.fetch = vi
        .fn()
        .mockResolvedValue(
          mockResponse({ ok: false, status: 409, json: conflictBody }),
        );

      try {
        await backupApi.importBackup(new File(["data"], "backup.zip"));
        expect.unreachable("should have thrown");
      } catch (e) {
        expect(e).toBeInstanceOf(Error);
        expect((e as Error).message).toBe("backup_conflict");
        // The .conflict property is what the UI reads to show the dialog
        expect((e as Error & { conflict: unknown }).conflict).toEqual(
          conflictBody,
        );
      }
    });

    it("on non-409 failure, throws an error containing the response text", async () => {
      global.fetch = vi
        .fn()
        .mockResolvedValue(
          mockResponse({ ok: false, status: 400, text: "Bad file" }),
        );

      await expect(
        backupApi.importBackup(new File(["data"], "backup.zip")),
      ).rejects.toThrow("Bad file");
    });

    it("returns parsed BackupMeta on success", async () => {
      const meta = { id: "b1", name: "n" };
      global.fetch = vi
        .fn()
        .mockResolvedValue(mockResponse({ ok: true, status: 200, json: meta }));

      const result = await backupApi.importBackup(
        new File(["data"], "backup.zip"),
      );
      expect(result).toEqual(meta);
    });
  });

  // =========================================================================
  // resolveImportConflict β€” error path
  // =========================================================================
  // Only the error branch needs guarding; the happy path is a thin fetch+json.

  describe("resolveImportConflict", () => {
    it("on non-ok response, throws an error containing the response text", async () => {
      global.fetch = vi
        .fn()
        .mockResolvedValue(
          mockResponse({ ok: false, status: 500, text: "server error" }),
        );

      await expect(backupApi.resolveImportConflict("tok-123")).rejects.toThrow(
        "server error",
      );
    });
  });

  // =========================================================================
  // exportBackup β€” cancel-suppression logic
  // =========================================================================
  // exportBackup delegates to downloadFileFromUrl, but crucially it **swallows**
  // DownloadCancelledError so the caller sees a clean void return when the user
  // cancels the save dialog.  This is a behavioural contract, not a transport
  // detail β€” if it regresses, the UI will show a spurious error on cancel.
  // =========================================================================

  describe("exportBackup", () => {
    it("returns undefined (does not rethrow) when download is cancelled", async () => {
      vi.mocked(downloadFileFromUrl).mockRejectedValueOnce(
        new DownloadCancelledError(),
      );

      await expect(
        backupApi.exportBackup("b1", "my-backup"),
      ).resolves.toBeUndefined();
    });

    it("re-throws non-cancelled errors so the UI can display them", async () => {
      vi.mocked(downloadFileFromUrl).mockRejectedValueOnce(
        new Error("Network failure"),
      );

      await expect(backupApi.exportBackup("b1", "n")).rejects.toThrow(
        "Network failure",
      );
    });
  });
});