Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

27.5 KB
Raw Blame
import {
  CodeBridgeStatusError,
  createCodeBridgeStatusPoller,
  getCodeBridgeWorkerStatus,
} from './bridge';

describe('getCodeBridgeWorkerStatus', () => {
  test.each([
    [['bash'], ['bash']],
    [['bash', 'python'], ['bash']],
    [['python'], undefined],
    [['bash', 123], undefined],
  ])(
    'preserves recognized programmatic capability from %j',
    async (programmaticLanguages, expected) => {
      const status = await getCodeBridgeWorkerStatus({
        baseURL: 'https://code.example.com/v1',
        token: 'token',
        workerId: 'personal-vm',
        fetchImpl: jest.fn().mockResolvedValue(
          Response.json({
            protocolVersion: 1,
            workerId: 'personal-vm',
            online: true,
            ready: true,
            leaseExpiresInMs: 45_000,
            capabilities: {
              statefulWorkspace: false,
              runtimes: [],
              sandboxProfile: 'native-srt',
              workspaceTools: {
                protocolVersion: 1,
                operations: ['execute_command'],
                workspaces: [{ id: 'project-a' }],
                programmaticLanguages,
              },
            },
          }),
        ),
      });
      expect(status.programmaticLanguages).toEqual(expected);
    },
  );
  test('accepts the maximum declared environment metadata population', async () => {
    const workspaces = Array.from({ length: 32 }, (_, index) => ({
      id: `root-${index}`,
      name: 'n'.repeat(128),
      environment: {
        fingerprint: 'a'.repeat(64),
        repo: `a/${'b'.repeat(254)}`,
        ref: '\t'.repeat(255) + 'x',
        actions: Array.from({ length: 32 }, (_, action) => `${action}${'a'.repeat(62)}`),
      },
    }));
    const payload = {
      protocolVersion: 1,
      workerId: 'personal-vm',
      online: true,
      ready: true,
      leaseExpiresInMs: 45000,
      capabilities: {
        statefulWorkspace: false,
        sandboxProfile: 'native-srt',
        runtimes: [],
        workspaceTools: { protocolVersion: 1, operations: ['execute_command'], workspaces },
      },
    };
    expect(Buffer.byteLength(JSON.stringify(payload))).toBeGreaterThan(64 * 1024);
    await expect(
      getCodeBridgeWorkerStatus({
        baseURL: 'https://code.example.com/v1',
        token: 'token',
        workerId: 'personal-vm',
        fetchImpl: jest.fn().mockResolvedValue(Response.json(payload)),
      }),
    ).resolves.toMatchObject({ status: 'ready', workspaces });
  });
  test('normalizes a ready worker while exposing only bounded capability metadata', async () => {
    const fetchImpl = jest.fn().mockResolvedValue(
      new Response(
        JSON.stringify({
          protocolVersion: 1,
          workerId: 'personal-vm',
          online: true,
          ready: true,
          leaseExpiresInMs: 45_000,
          maxCommandTimeoutMs: 120_000,
          capabilities: {
            statefulWorkspace: true,
            sandboxProfile: 'native-srt',
            runtimes: ['bash'],
            workspaceTools: {
              protocolVersion: 1,
              operations: ['read_file', 'execute_command'],
              workspaces: [
                {
                  id: 'project-a',
                  name: 'Project A',
                  workspaceInstances: ['git_worktree'],
                  workspaceScopes: ['git_linked_worktree'],
                },
                { id: 'docs', operations: ['read_file'] },
              ],
            },
            identityId: 'must-not-cross-the-boundary',
          },
          binding: { tenantId: 'tenant-1', principal: { type: 'user', id: 'user-1' } },
        }),
      ),
    );

    await expect(
      getCodeBridgeWorkerStatus({
        baseURL: 'https://code.example.com/v1/',
        token: 'administrator-token',
        workerId: 'personal-vm',
        fetchImpl,
      }),
    ).resolves.toEqual({
      status: 'ready',
      statefulWorkspace: true,
      leaseExpiresInMs: 45_000,
      maxCommandTimeoutMs: 120_000,
      sandboxProfile: 'native-srt',
      runtimes: ['bash'],
      operations: ['read_file', 'execute_command'],
      workspaces: [
        {
          id: 'project-a',
          name: 'Project A',
          workspaceInstances: ['git_worktree'],
          workspaceScopes: ['git_linked_worktree'],
        },
        { id: 'docs', operations: ['read_file'] },
      ],
    });
    expect(fetchImpl).toHaveBeenCalledWith(
      'https://code.example.com/v1/bridge/workers/personal-vm/status',
      expect.objectContaining({
        headers: { Authorization: 'Bearer administrator-token' },
        redirect: 'error',
      }),
    );
  });

  test('carries negotiated edit features and drops names it does not know', async () => {
    const fetchImpl = jest.fn().mockResolvedValue(
      new Response(
        JSON.stringify({
          protocolVersion: 1,
          workerId: 'personal-vm',
          online: true,
          ready: true,
          leaseExpiresInMs: 45_000,
          capabilities: {
            statefulWorkspace: true,
            sandboxProfile: 'native-srt',
            runtimes: ['bash'],
            workspaceTools: {
              protocolVersion: 1,
              operations: ['read_file', 'edit_file'],
              workspaces: [{ id: 'project-a' }],
              editFileFeatures: ['replace_all', 'future_feature', 'tolerant_match'],
            },
          },
        }),
      ),
    );

    const status = await getCodeBridgeWorkerStatus({
      baseURL: 'https://code.example.com/v1/',
      token: 'administrator-token',
      workerId: 'personal-vm',
      fetchImpl,
    });

    expect(status.editFileFeatures).toEqual(['tolerant_match', 'replace_all']);
  });

  test('keeps legacy worker status readable without inventing a primary workspace', async () => {
    const fetchImpl = jest.fn().mockResolvedValue(
      new Response(
        JSON.stringify({
          protocolVersion: 1,
          workerId: 'personal-vm',
          online: true,
          ready: true,
          leaseExpiresInMs: 45_000,
          capabilities: {
            statefulWorkspace: true,
            sandboxProfile: 'native-srt',
            runtimes: ['bash'],
            workspaceTools: { operations: ['read_file', 'execute_command'] },
          },
        }),
      ),
    );

    await expect(
      getCodeBridgeWorkerStatus({
        baseURL: 'https://code.example.com/v1',
        token: 'administrator-token',
        workerId: 'personal-vm',
        fetchImpl,
      }),
    ).resolves.toEqual({
      status: 'ready',
      statefulWorkspace: true,
      leaseExpiresInMs: 45_000,
      sandboxProfile: 'native-srt',
      runtimes: ['bash'],
      operations: ['read_file', 'execute_command'],
    });
  });

  test.each([
    { online: false, ready: true },
    { online: true, ready: false },
    { online: false, ready: false, leaseExpiresInMs: 5_000 },
    { online: true, ready: true, leaseExpiresInMs: 60_001 },
    { online: true, ready: true, maxCommandTimeoutMs: 0 },
    { online: true, ready: true, maxCommandTimeoutMs: 300_001 },
    {
      online: true,
      ready: true,
      capabilities: { sandboxProfile: 'native-srt', runtimes: Array(33).fill('bash') },
    },
    {
      online: true,
      ready: true,
      leaseExpiresInMs: 5_000,
      capabilities: {
        statefulWorkspace: true,
        sandboxProfile: 'native-srt',
        runtimes: ['bash'],
        workspaceTools: {
          protocolVersion: 1,
          operations: ['read_file'],
          workspaces: [{ id: '../escape' }],
        },
      },
    },
    {
      online: true,
      ready: true,
      leaseExpiresInMs: 5_000,
      capabilities: {
        statefulWorkspace: true,
        sandboxProfile: 'native-srt',
        runtimes: ['bash'],
        workspaceTools: {
          protocolVersion: 1,
          operations: ['read_file'],
          workspaces: [{ id: 'project-a', workspaceInstances: ['container'] }],
        },
      },
    },
    {
      online: true,
      ready: true,
      leaseExpiresInMs: 5_000,
      capabilities: {
        statefulWorkspace: true,
        sandboxProfile: 'native-srt',
        runtimes: ['bash'],
        workspaceTools: {
          protocolVersion: 1,
          operations: ['read_file'],
          workspaces: [{ id: 'project-a', operations: ['execute_command'] }],
        },
      },
    },
    {
      online: true,
      ready: true,
      leaseExpiresInMs: 5_000,
      capabilities: {
        statefulWorkspace: true,
        sandboxProfile: 'native-srt',
        runtimes: ['bash'],
        workspaceTools: {
          protocolVersion: 1,
          operations: ['read_file'],
          workspaces: [{ id: 'project-a', operations: null }],
        },
      },
    },
    {
      online: true,
      ready: true,
      leaseExpiresInMs: 5_000,
      capabilities: {
        statefulWorkspace: true,
        sandboxProfile: 'native-srt',
        runtimes: ['bash'],
        workspaceTools: {
          protocolVersion: 1,
          operations: ['read_file'],
          workspaces: [{ id: 'project-a', workspaceScopes: ['subdirectory'] }],
        },
      },
    },
    {
      online: true,
      ready: true,
      leaseExpiresInMs: 5_000,
      capabilities: {
        statefulWorkspace: true,
        sandboxProfile: 'native-srt',
        runtimes: ['bash'],
        workspaceTools: {
          protocolVersion: 1,
          operations: ['read_file'],
          workspaces: [{ id: 'project-a', workspaceScopes: 'git_linked_worktree' }],
        },
      },
    },
    {
      online: true,
      ready: true,
      leaseExpiresInMs: 5_000,
      capabilities: {
        statefulWorkspace: true,
        sandboxProfile: 'native-srt',
        runtimes: ['bash'],
        workspaceTools: {
          protocolVersion: 1,
          operations: ['read_file'],
          workspaces: [
            { id: 'project-a', workspaceScopes: ['git_linked_worktree', 'git_linked_worktree'] },
          ],
        },
      },
    },
  ])('rejects an invalid upstream status response: %p', async (invalid) => {
    const fetchImpl = jest
      .fn()
      .mockResolvedValue(
        new Response(JSON.stringify({ protocolVersion: 1, workerId: 'personal-vm', ...invalid })),
      );

    await expect(
      getCodeBridgeWorkerStatus({
        baseURL: 'https://code.example.com/v1',
        token: 'administrator-token',
        workerId: 'personal-vm',
        fetchImpl,
      }),
    ).rejects.toEqual(
      expect.objectContaining<Partial<CodeBridgeStatusError>>({ reason: 'invalid' }),
    );
  });

  describe('workspace operations a newer worker advertises', () => {
    const statusWith = (workspaceTools: object) =>
      getCodeBridgeWorkerStatus({
        baseURL: 'https://code.example.com/v1',
        token: 'token',
        workerId: 'personal-vm',
        fetchImpl: jest.fn().mockResolvedValue(
          Response.json({
            protocolVersion: 1,
            workerId: 'personal-vm',
            online: true,
            ready: true,
            leaseExpiresInMs: 45_000,
            capabilities: {
              statefulWorkspace: false,
              runtimes: [],
              sandboxProfile: 'native-srt',
              workspaceTools,
            },
          }),
        ),
      });

    test('drops names this build does not know and keeps the workspace usable', async () => {
      await expect(
        statusWith({
          protocolVersion: 1,
          operations: ['read_file', 'stream_logs', 'write_file', 'import_file'],
          workspaces: [
            { id: 'project-a' },
            { id: 'docs', operations: ['read_file', 'stream_logs'] },
            { id: 'future', operations: ['stream_logs'] },
          ],
        }),
      ).resolves.toEqual(
        expect.objectContaining({
          status: 'ready',
          operations: ['read_file', 'write_file', 'import_file'],
          workspaces: [
            { id: 'project-a' },
            { id: 'docs', operations: ['read_file'] },
            { id: 'future', operations: [] },
          ],
        }),
      );
    });

    test('drops unknown names from a legacy capability list too', async () => {
      await expect(statusWith({ operations: ['read_file', 'stream_logs'] })).resolves.toEqual(
        expect.objectContaining({ operations: ['read_file'] }),
      );
    });

    test('treats a worker offering only unknown operations as offering no workspace tools', async () => {
      const status = await statusWith({
        protocolVersion: 1,
        operations: ['stream_logs'],
        workspaces: [{ id: 'project-a' }],
      });
      expect(status.status).toBe('ready');
      expect(status.operations).toBeUndefined();
      expect(status.workspaces).toBeUndefined();
    });

    test.each([
      ['a non-string name', ['read_file', 42]],
      ['a malformed name', ['read_file', 'Read File']],
      ['a duplicate name', ['read_file', 'read_file']],
      ['an unbounded list', ['read_file', ...Array.from({ length: 64 }, (_, i) => `op_${i}`)]],
    ])('still rejects %s', async (_label, operations) => {
      await expect(
        statusWith({ protocolVersion: 1, operations, workspaces: [{ id: 'project-a' }] }),
      ).rejects.toEqual(
        expect.objectContaining<Partial<CodeBridgeStatusError>>({ reason: 'invalid' }),
      );
    });

    test('still rejects a root claiming a known operation the worker does not offer', async () => {
      await expect(
        statusWith({
          protocolVersion: 1,
          operations: ['read_file', 'stream_logs'],
          workspaces: [{ id: 'project-a', operations: ['execute_command'] }],
        }),
      ).rejects.toEqual(
        expect.objectContaining<Partial<CodeBridgeStatusError>>({ reason: 'invalid' }),
      );
    });
  });

  test('rejects an upstream response before buffering more than 256 KiB', async () => {
    const fetchImpl = jest
      .fn()
      .mockResolvedValue(new Response(JSON.stringify({ ignored: 'x'.repeat(257 * 1024) })));

    await expect(
      getCodeBridgeWorkerStatus({
        baseURL: 'https://code.example.com/v1',
        token: 'administrator-token',
        workerId: 'personal-vm',
        fetchImpl,
      }),
    ).rejects.toEqual(
      expect.objectContaining<Partial<CodeBridgeStatusError>>({ reason: 'invalid' }),
    );
  });

  test('coalesces matching polls and rejects excess distinct upstream concurrency', async () => {
    let release: ((response: Response) => void) | undefined;
    const fetchImpl = jest.fn(
      () =>
        new Promise<Response>((resolve) => {
          release = resolve;
        }),
    );
    const poll = createCodeBridgeStatusPoller({ fetchImpl, maxConcurrent: 1 });
    const params = {
      baseURL: 'https://code.example.com/v1',
      token: 'administrator-token',
      workerId: 'personal-vm',
    };

    const first = poll(params);
    expect(poll(params)).toBe(first);
    await expect(poll({ ...params, workerId: 'second-vm' })).rejects.toEqual(
      expect.objectContaining<Partial<CodeBridgeStatusError>>({ reason: 'busy' }),
    );
    release?.(
      new Response(
        JSON.stringify({
          protocolVersion: 1,
          workerId: 'personal-vm',
          online: false,
          ready: false,
        }),
      ),
    );
    await expect(first).resolves.toEqual({ status: 'offline' });
    expect(fetchImpl).toHaveBeenCalledTimes(1);
  });

  test('bypasses cached status without falling back on failure or changing normal polling', async () => {
    jest.spyOn(Date, 'now').mockReturnValue(1_000);
    const offline = () =>
      new Response(
        JSON.stringify({
          protocolVersion: 1,
          workerId: 'personal-vm',
          online: false,
          ready: false,
        }),
      );
    const fetchImpl = jest
      .fn()
      .mockImplementationOnce(async () => offline())
      .mockRejectedValueOnce(new Error('offline'))
      .mockImplementation(async () => offline());
    const poll = createCodeBridgeStatusPoller({ fetchImpl, maxConcurrent: 1 });
    const params = {
      baseURL: 'https://code.example.com/v1',
      token: 'administrator-token',
      workerId: 'personal-vm',
    };
    await expect(poll(params)).resolves.toEqual({ status: 'offline' });
    await expect(poll({ ...params, bypassCache: true })).rejects.toEqual(
      expect.objectContaining({ reason: 'failed' }),
    );
    await expect(poll({ ...params, bypassCache: true })).resolves.toEqual({ status: 'offline' });
    expect(fetchImpl).toHaveBeenCalledTimes(3);
    await expect(poll(params)).resolves.toEqual({ status: 'offline' });
    expect(fetchImpl).toHaveBeenCalledTimes(3);
  });

  test('never joins an older in-flight poll and shares its upstream concurrency limit', async () => {
    const responses: Array<(response: Response) => void> = [];
    const fetchImpl = jest.fn(() => new Promise<Response>((resolve) => responses.push(resolve)));
    const poll = createCodeBridgeStatusPoller({ fetchImpl, maxConcurrent: 2 });
    const params = {
      baseURL: 'https://code.example.com/v1',
      token: 'administrator-token',
      workerId: 'personal-vm',
    };
    const previous = poll(params);
    const current = poll({ ...params, bypassCache: true });
    expect(current).not.toBe(previous);
    expect(poll(params)).toBe(previous);
    expect(fetchImpl).toHaveBeenCalledTimes(2);
    await expect(poll({ ...params, bypassCache: true })).rejects.toEqual(
      expect.objectContaining({ reason: 'busy' }),
    );
    await expect(poll({ ...params, workerId: 'second-vm' })).rejects.toEqual(
      expect.objectContaining({ reason: 'busy' }),
    );
    responses[1](
      new Response(
        JSON.stringify({
          protocolVersion: 1,
          workerId: 'personal-vm',
          online: true,
          ready: true,
          leaseExpiresInMs: 50_000,
          capabilities: { sandboxProfile: 'native-srt', runtimes: ['bash'] },
        }),
      ),
    );
    await expect(current).resolves.toEqual(expect.objectContaining({ status: 'ready' }));
    responses[0](
      new Response(
        JSON.stringify({
          protocolVersion: 1,
          workerId: 'personal-vm',
          online: false,
          ready: false,
        }),
      ),
    );
    await expect(previous).resolves.toEqual({ status: 'offline' });
    const next = poll({ ...params, bypassCache: true });
    expect(fetchImpl).toHaveBeenCalledTimes(3);
    responses[2](
      new Response(
        JSON.stringify({
          protocolVersion: 1,
          workerId: 'personal-vm',
          online: false,
          ready: false,
        }),
      ),
    );
    await next;
  });

  test('does not coalesce status requests across credential rotations', async () => {
    const fetchImpl = jest.fn().mockImplementation((_input, init) =>
      Promise.resolve(
        new Response(
          JSON.stringify({
            protocolVersion: 1,
            workerId: 'personal-vm',
            online: false,
            ready: false,
            authorization: (init?.headers as Record<string, string>).Authorization,
          }),
        ),
      ),
    );
    const poll = createCodeBridgeStatusPoller({ fetchImpl });
    const params = {
      baseURL: 'https://code.example.com/v1',
      workerId: 'personal-vm',
    };

    await Promise.all([
      poll({ ...params, token: 'old-administrator-token' }),
      poll({ ...params, token: 'new-administrator-token' }),
    ]);

    expect(fetchImpl).toHaveBeenCalledTimes(2);
  });

  test('subtracts polling time when caching within the remaining worker lease', async () => {
    let now = 1_000;
    jest.spyOn(Date, 'now').mockImplementation(() => now);
    const fetchImpl = jest.fn().mockImplementation(() =>
      Promise.resolve(
        new Response(
          JSON.stringify({
            protocolVersion: 1,
            workerId: 'personal-vm',
            online: true,
            ready: true,
            leaseExpiresInMs: 5,
            capabilities: { sandboxProfile: 'native-srt', runtimes: ['bash'] },
          }),
        ),
      ),
    );
    const poll = createCodeBridgeStatusPoller({ fetchImpl, cacheTtlMs: 2_000 });
    const params = {
      baseURL: 'https://code.example.com/v1',
      token: 'administrator-token',
      workerId: 'personal-vm',
    };

    const first = poll(params);
    now += 4;
    await first;
    now += 2;
    await poll(params);

    expect(fetchImpl).toHaveBeenCalledTimes(2);
    jest.restoreAllMocks();
  });

  test('holds upstream capacity until a rejected response body is cancelled', async () => {
    let finishCancellation: (() => void) | undefined;
    const rejectedBody = new ReadableStream({
      cancel: () =>
        new Promise<void>((resolve) => {
          finishCancellation = resolve;
        }),
    });
    const fetchImpl = jest
      .fn()
      .mockResolvedValueOnce(new Response(rejectedBody, { status: 403 }))
      .mockResolvedValue(
        new Response(
          JSON.stringify({
            protocolVersion: 1,
            workerId: 'second-vm',
            online: false,
            ready: false,
          }),
        ),
      );
    const poll = createCodeBridgeStatusPoller({ fetchImpl, maxConcurrent: 1 });
    const params = {
      baseURL: 'https://code.example.com/v1',
      token: 'administrator-token',
      workerId: 'personal-vm',
    };

    const first = poll(params);
    await new Promise<void>((resolve) => setImmediate(resolve));
    await expect(poll({ ...params, workerId: 'second-vm' })).rejects.toEqual(
      expect.objectContaining<Partial<CodeBridgeStatusError>>({ reason: 'busy' }),
    );
    finishCancellation?.();
    await expect(first).rejects.toEqual(
      expect.objectContaining<Partial<CodeBridgeStatusError>>({ reason: 'rejected' }),
    );
    await expect(poll({ ...params, workerId: 'second-vm' })).resolves.toEqual({
      status: 'offline',
    });
  });
});

describe('createCodeBridgeStatusPoller transient failures', () => {
  const params = {
    baseURL: 'https://code.example.com/v1',
    token: 'administrator-token',
    workerId: 'personal-vm',
  };

  const statusResponse = (online: boolean, ready: boolean, leaseExpiresInMs = 60_000) =>
    new Response(
      JSON.stringify({
        protocolVersion: 1,
        workerId: 'personal-vm',
        online,
        ready,
        ...(online
          ? { leaseExpiresInMs, capabilities: { sandboxProfile: 'native-srt', runtimes: ['bash'] } }
          : {}),
      }),
    );
  const networkFailure = () => Promise.reject(new TypeError('fetch failed'));

  let now = 10_000;
  beforeEach(() => {
    now = 10_000;
    jest.spyOn(Date, 'now').mockImplementation(() => now);
  });
  afterEach(() => {
    jest.restoreAllMocks();
  });

  test('answers a failed poll with the last ready status while its lease runs', async () => {
    const fetchImpl = jest
      .fn()
      .mockResolvedValueOnce(statusResponse(true, true, 60_000))
      .mockImplementationOnce(networkFailure);
    const poll = createCodeBridgeStatusPoller({ fetchImpl, cacheTtlMs: 0 });

    await expect(poll(params)).resolves.toMatchObject({ status: 'ready' });
    now += 15_000;
    await expect(poll(params)).resolves.toMatchObject({
      status: 'ready',
      leaseExpiresInMs: 45_000,
    });

    expect(fetchImpl).toHaveBeenCalledTimes(2);
  });

  test('treats a 5xx from the proxy in front of the Code API as transient', async () => {
    const fetchImpl = jest
      .fn()
      .mockResolvedValueOnce(statusResponse(true, true))
      .mockResolvedValueOnce(new Response('<html>Bad gateway</html>', { status: 502 }));
    const poll = createCodeBridgeStatusPoller({ fetchImpl, cacheTtlMs: 0 });

    await poll(params);
    now += 1_000;
    await expect(poll(params)).resolves.toMatchObject({ status: 'ready' });
  });

  test('retries once when no ready status is remembered', async () => {
    const fetchImpl = jest
      .fn()
      .mockImplementationOnce(networkFailure)
      .mockResolvedValueOnce(statusResponse(true, true));
    const poll = createCodeBridgeStatusPoller({ fetchImpl, cacheTtlMs: 0 });

    await expect(poll(params)).resolves.toMatchObject({ status: 'ready' });
    expect(fetchImpl).toHaveBeenCalledTimes(2);
  });

  test('fails after one retry once the remembered lease has run out', async () => {
    const fetchImpl = jest
      .fn()
      .mockResolvedValueOnce(statusResponse(true, true, 5_000))
      .mockImplementation(networkFailure);
    const poll = createCodeBridgeStatusPoller({ fetchImpl, cacheTtlMs: 0 });

    await poll(params);
    now += 5_000;
    await expect(poll(params)).rejects.toEqual(
      expect.objectContaining<Partial<CodeBridgeStatusError>>({ reason: 'failed' }),
    );
    expect(fetchImpl).toHaveBeenCalledTimes(3);
  });

  test('never covers a rejection, and forgets the remembered status after one', async () => {
    const fetchImpl = jest
      .fn()
      .mockResolvedValueOnce(statusResponse(true, true))
      .mockResolvedValueOnce(new Response('{}', { status: 401 }))
      .mockImplementation(networkFailure);
    const poll = createCodeBridgeStatusPoller({ fetchImpl, cacheTtlMs: 0 });

    await poll(params);
    await expect(poll(params)).rejects.toEqual(
      expect.objectContaining<Partial<CodeBridgeStatusError>>({ reason: 'rejected' }),
    );
    await expect(poll(params)).rejects.toEqual(
      expect.objectContaining<Partial<CodeBridgeStatusError>>({ reason: 'failed' }),
    );
    expect(fetchImpl).toHaveBeenCalledTimes(4);
  });

  test('forgets a ready status once the worker reports it is no longer ready', async () => {
    const fetchImpl = jest
      .fn()
      .mockResolvedValueOnce(statusResponse(true, true))
      .mockResolvedValueOnce(statusResponse(false, false))
      .mockImplementation(networkFailure);
    const poll = createCodeBridgeStatusPoller({ fetchImpl, cacheTtlMs: 0 });

    await poll(params);
    await expect(poll(params)).resolves.toMatchObject({ status: 'offline' });
    await expect(poll(params)).rejects.toEqual(
      expect.objectContaining<Partial<CodeBridgeStatusError>>({ reason: 'failed' }),
    );
  });

  test('gives bypassCache callers one fresh observation, never the remembered status', async () => {
    const fetchImpl = jest
      .fn()
      .mockResolvedValueOnce(statusResponse(true, true))
      .mockImplementationOnce(networkFailure)
      .mockResolvedValueOnce(statusResponse(true, false));
    const poll = createCodeBridgeStatusPoller({ fetchImpl, cacheTtlMs: 0 });

    await poll(params);
    await expect(poll({ ...params, bypassCache: true })).rejects.toEqual(
      expect.objectContaining<Partial<CodeBridgeStatusError>>({ reason: 'failed' }),
    );
    await expect(poll({ ...params, bypassCache: true })).resolves.toMatchObject({
      status: 'starting',
    });
    expect(fetchImpl).toHaveBeenCalledTimes(3);
  });

  test('forgets the remembered status when a selection check is rejected', async () => {
    const fetchImpl = jest
      .fn()
      .mockResolvedValueOnce(statusResponse(true, true))
      .mockResolvedValueOnce(new Response('{}', { status: 404 }))
      .mockImplementation(networkFailure);
    const poll = createCodeBridgeStatusPoller({ fetchImpl, cacheTtlMs: 0 });

    await poll(params);
    await expect(poll({ ...params, bypassCache: true })).rejects.toEqual(
      expect.objectContaining<Partial<CodeBridgeStatusError>>({ reason: 'rejected' }),
    );
    await expect(poll(params)).rejects.toEqual(
      expect.objectContaining<Partial<CodeBridgeStatusError>>({ reason: 'failed' }),
    );
  });

  test('keeps remembered statuses separate per credential', async () => {
    const fetchImpl = jest
      .fn()
      .mockResolvedValueOnce(statusResponse(true, true))
      .mockImplementation(networkFailure);
    const poll = createCodeBridgeStatusPoller({ fetchImpl, cacheTtlMs: 0 });

    await poll(params);
    await expect(poll({ ...params, token: 'rotated-administrator-token' })).rejects.toEqual(
      expect.objectContaining<Partial<CodeBridgeStatusError>>({ reason: 'failed' }),
    );
  });
});