/** * Tests for the retry options `bin/quak.ts` loads the saved session with: * `quak backup` gets the unattended ones, every other command the default. * * Each test runs `bin/quak.ts`, as the smoke test does, with its session loader * replaced by one that records the options it is given and reports no session, * so the command stops before it makes any request. */ import { mkdtempSync, rmSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { afterAll, afterEach, describe, expect, it, vi } from "vitest"; import type { ApiClientOptions } from "../../src/api/client.js"; import { UNATTENDED_RETRY_OPTIONS } from "../../src/retry.js"; const loaded = vi.hoisted(() => [] as (ApiClientOptions | undefined)[]); vi.mock("../../src/cli-session.js", () => ({ loadSession: (_path: string, apiOptions?: ApiClientOptions) => { loaded.push(apiOptions); return null; }, })); const argv = process.argv; const dir = mkdtempSync(join(tmpdir(), "quak-bin-test-")); // Run `quak ` to completion. The "Not logged in" message and the exit // are swallowed. const quak = async (...args: string[]): Promise => { vi.spyOn(process.stderr, "write").mockImplementation(() => true); vi.spyOn(process, "exit").mockImplementation(() => undefined as never); process.argv = ["node", "quak", ...args]; vi.resetModules(); await import("../../bin/quak.js"); }; afterEach(() => { process.argv = argv; loaded.length = 0; vi.restoreAllMocks(); }); afterAll(() => { rmSync(dir, { recursive: true, force: true }); }); describe("bin/quak.ts session loading", () => { it("loads the session for backup with the unattended retry options", async () => { await quak("backup", dir); // `vi.resetModules()` gave `bin/quak.ts` its own copy of // `src/retry.ts`, whose `sleep` is a different function, so the // options are compared by their numbers. const { attempts, baseDelayMs, maxDelayMs } = UNATTENDED_RETRY_OPTIONS; expect(loaded).toEqual([ { retry: expect.objectContaining({ attempts, baseDelayMs, maxDelayMs, }), }, ]); }); it("loads the session for another command with the default options", async () => { await quak("collections"); expect(loaded).toEqual([undefined]); }); });