File size: 1,146 Bytes
a080db2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { describe, expect, test } from "bun:test";

import type { HttpClient } from "../../http";
import { systemTimers, type CloudClientTimers } from "../../timers";
import { Subscriptions } from "./subscriptions";

describe("Subscriptions", () => {
  test("uses the injected scheduler for stale-session retries", async () => {
    const scheduledDelays: number[] = [];
    const timers: CloudClientTimers = {
      ...systemTimers,
      setTimeout(callback, delayMs) {
        scheduledDelays.push(delayMs);
        callback();
        return { kind: "subscription-retry" };
      },
    };
    let calls = 0;
    const http = {
      async put() {
        calls += 1;
        return calls === 1
          ? { applied: false, version: 1, reason: "stale-session" }
          : { applied: true, version: 1, reason: null };
      },
    } as unknown as HttpClient;
    const subscriptions = new Subscriptions({ http, timers });

    await subscriptions.set([{ kind: "transcription", language: { mode: "auto" } }], "session-1");

    expect(calls).toBe(2);
    expect(scheduledDelays).toEqual([5_000]);
  });
});