Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

14.9 KB
Raw Blame
import { DEV_FALLBACK_CLI_REFUSAL } from '@inkeep/open-knowledge-core';
import { afterEach, describe, expect, test, vi } from 'vitest';
import { httpAuthTransport } from './auth-transport';

const realFetch = globalThis.fetch;
afterEach(() => {
  globalThis.fetch = realFetch;
});

async function collectEvents(handle: {
  events: AsyncIterable<unknown>;
}): Promise<Array<Record<string, unknown>>> {
  const out: Array<Record<string, unknown>> = [];
  for await (const e of handle.events) out.push(e as Record<string, unknown>);
  return out;
}

function ndjsonResponse(lines: string[]): Response {
  return new Response(new Blob([lines.map((l) => `${l}\n`).join('')]).stream(), { status: 200 });
}

describe('httpAuthTransport().pat', () => {
  test('POSTs { host, token } to the pat relay and returns the login on success', async () => {
    const calls: Array<{ url: string; body: unknown }> = [];
    globalThis.fetch = vi.fn(async (url: string, init?: RequestInit) => {
      calls.push({ url, body: JSON.parse(String(init?.body)) });
      return new Response(JSON.stringify({ host: 'ghes.acme.test', login: 'omar-acme' }), {
        status: 200,
      });
    }) as unknown as typeof fetch;

    const result = await httpAuthTransport().pat?.('ghes.acme.test', 'ghp_secret');
    expect(result).toEqual({ ok: true, login: 'omar-acme' });
    expect(calls).toHaveLength(1);
    expect(calls[0]?.url).toBe('/api/local-op/auth/pat');
    expect(calls[0]?.body).toEqual({ host: 'ghes.acme.test', token: 'ghp_secret' });
  });

  test('surfaces the problem+json detail on a rejected token (bounded reason)', async () => {
    globalThis.fetch = vi.fn(
      async () =>
        new Response(
          JSON.stringify({
            type: 'urn:ok:error:auth-failed',
            title: 'Authentication failed',
            status: 400,
            detail: 'Token invalid for ghes.acme.test',
          }),
          { status: 400, headers: { 'Content-Type': 'application/problem+json' } },
        ),
    ) as unknown as typeof fetch;

    const result = await httpAuthTransport().pat?.('ghes.acme.test', 'bad');
    expect(result).toEqual({ ok: false, error: 'Token invalid for ghes.acme.test' });
  });

  test('returns a generic connection error when the request throws', async () => {
    globalThis.fetch = vi.fn(async () => {
      throw new Error('network down');
    }) as unknown as typeof fetch;

    const result = await httpAuthTransport().pat?.('ghes.acme.test', 'x');
    expect(result?.ok).toBe(false);
    expect(result?.error).toBe('Connection error — try again');
  });
});

describe('httpAuthTransport().hostToken', () => {
  test('POSTs { host, username, token } to the token relay and returns the login on success', async () => {
    const calls: Array<{ url: string; body: unknown }> = [];
    globalThis.fetch = vi.fn(async (url: string, init?: RequestInit) => {
      calls.push({ url, body: JSON.parse(String(init?.body)) });
      return new Response(JSON.stringify({ host: 'gitea.internal', login: 'oauth2' }), {
        status: 200,
      });
    }) as unknown as typeof fetch;

    const result = await httpAuthTransport().hostToken?.(
      'gitea.internal',
      'oauth2',
      'glpat_secret',
    );
    expect(result).toEqual({ ok: true, login: 'oauth2' });
    expect(calls).toHaveLength(1);
    expect(calls[0]?.url).toBe('/api/local-op/auth/token');
    expect(calls[0]?.body).toEqual({
      host: 'gitea.internal',
      username: 'oauth2',
      token: 'glpat_secret',
    });
  });

  test('surfaces the problem+json detail on a rejected request (bounded reason)', async () => {
    globalThis.fetch = vi.fn(
      async () =>
        new Response(
          JSON.stringify({
            type: 'urn:ok:error:auth-failed',
            title: 'Authentication failed',
            status: 400,
            detail: 'Host must not include a scheme',
          }),
          { status: 400, headers: { 'Content-Type': 'application/problem+json' } },
        ),
    ) as unknown as typeof fetch;

    const result = await httpAuthTransport().hostToken?.('https://gitea.internal', 'oauth2', 'x');
    expect(result).toEqual({ ok: false, error: 'Host must not include a scheme' });
  });

  test('returns a generic connection error when the request throws', async () => {
    globalThis.fetch = vi.fn(async () => {
      throw new Error('network down');
    }) as unknown as typeof fetch;

    const result = await httpAuthTransport().hostToken?.('gitea.internal', 'oauth2', 'x');
    expect(result?.ok).toBe(false);
    expect(result?.error).toBe('Connection error — try again');
  });
});

describe('httpAuthTransport().start / ghLogin (streamAuthEndpoint)', () => {
  test('a pre-stream problem+json failure surfaces the typed title as a single error event', async () => {
    globalThis.fetch = vi.fn(
      async () =>
        new Response(
          JSON.stringify({
            type: 'urn:ok:error:auth-failed',
            title: 'The GitHub CLI (gh) is not installed.',
            status: 400,
          }),
          { status: 400, headers: { 'Content-Type': 'application/problem+json' } },
        ),
    ) as unknown as typeof fetch;

    const events = await collectEvents(httpAuthTransport().ghLogin?.('ghes.acme.test') as never);
    expect(events).toEqual([{ type: 'error', message: 'The GitHub CLI (gh) is not installed.' }]);
  });

  test('a refused sign-in exposes the host-specific remediation detail', async () => {
    globalThis.fetch = vi.fn(
      async () =>
        new Response(
          JSON.stringify({
            type: 'urn:ok:error:non-github-origin',
            title: 'GitHub sign-in is unavailable for this remote.',
            status: 409,
            detail:
              'Declare ghes.example.test in ~/.ok/global.yml to use GitHub Enterprise sign-in.',
          }),
          { status: 409 },
        ),
    ) as unknown as typeof fetch;
    expect(await collectEvents(httpAuthTransport().start())).toEqual([
      {
        type: 'error',
        message: 'Declare ghes.example.test in ~/.ok/global.yml to use GitHub Enterprise sign-in.',
      },
    ]);
  });

  test('a dev build without its workspace CLI shows the build-and-restart refusal as the sign-in error', async () => {
    globalThis.fetch = vi.fn(
      async () =>
        new Response(
          JSON.stringify({
            type: 'urn:ok:error:auth-failed',
            title: 'This dev build has no built CLI.',
            status: 409,
            detail: DEV_FALLBACK_CLI_REFUSAL,
          }),
          { status: 409, headers: { 'Content-Type': 'application/problem+json' } },
        ),
    ) as unknown as typeof fetch;
    expect(await collectEvents(httpAuthTransport().start())).toEqual([
      { type: 'error', message: DEV_FALLBACK_CLI_REFUSAL },
    ]);
  });

  test('a pre-stream failure with an unparseable body falls back to the generic message', async () => {
    globalThis.fetch = vi.fn(
      async () => new Response('<html>gateway error</html>', { status: 502 }),
    ) as unknown as typeof fetch;

    const events = await collectEvents(httpAuthTransport().start() as never);
    expect(events).toEqual([{ type: 'error', message: 'Failed to start sign-in — try again' }]);
  });

  test('streams verification then complete, ending iteration on the terminal event', async () => {
    globalThis.fetch = vi.fn(async () =>
      ndjsonResponse([
        JSON.stringify({
          type: 'verification',
          user_code: 'AB-12',
          verification_uri: 'https://github.com/login/device',
          expires_in: 900,
        }),
        JSON.stringify({ type: 'complete', host: 'github.com', login: 'octocat' }),
      ]),
    ) as unknown as typeof fetch;

    const events = await collectEvents(httpAuthTransport().start() as never);
    expect(events.map((e) => e.type)).toEqual(['verification', 'complete']);
    expect(events[1]?.login).toBe('octocat');
  });

  test('a mid-stream {type:error, problem} line is bridged to {type:error, message}', async () => {
    globalThis.fetch = vi.fn(async () =>
      ndjsonResponse([
        JSON.stringify({
          type: 'error',
          problem: { title: 'Authentication failed', detail: 'Device flow was denied' },
        }),
      ]),
    ) as unknown as typeof fetch;

    const events = await collectEvents(httpAuthTransport().start() as never);
    expect(events).toEqual([{ type: 'error', message: 'Device flow was denied' }]);
  });

  test('a stream that ends before any code is issued surfaces the no-confirmation error', async () => {
    globalThis.fetch = vi.fn(async () => ndjsonResponse([])) as unknown as typeof fetch;

    const events = await collectEvents(httpAuthTransport().start() as never);
    expect(events.map((e) => e.type)).toEqual(['error']);
    expect(events[0]?.message).toContain('without confirmation');
  });

  test('keepalive ping lines are ignored — not surfaced, not terminal', async () => {
    globalThis.fetch = vi.fn(async () =>
      ndjsonResponse([
        JSON.stringify({ type: 'ping' }),
        JSON.stringify({
          type: 'verification',
          user_code: 'AB-12',
          verification_uri: 'https://github.com/login/device',
          expires_in: 900,
        }),
        JSON.stringify({ type: 'ping' }),
        JSON.stringify({ type: 'ping' }),
        JSON.stringify({ type: 'complete', host: 'github.com', login: 'octocat' }),
      ]),
    ) as unknown as typeof fetch;

    const events = await collectEvents(httpAuthTransport().start() as never);
    expect(events.map((e) => e.type)).toEqual(['verification', 'complete']);
  });
});

describe('streamAuthEndpoint — recovery after a mid-flow stream drop', () => {
  const VERIFICATION = JSON.stringify({
    type: 'verification',
    user_code: 'AB-12',
    verification_uri: 'https://github.com/login/device',
    expires_in: 900,
  });

  function severedAfterVerification(): Response {
    let sent = false;
    return new Response(
      new ReadableStream<Uint8Array>({
        pull(controller) {
          if (!sent) {
            sent = true;
            controller.enqueue(new TextEncoder().encode(`${VERIFICATION}\n`));
            return;
          }
          controller.error(new TypeError('network error'));
        },
      }),
      { status: 200 },
    );
  }

  test('a severed stream recovers via the status poll once the token lands', async () => {
    let statusCalls = 0;
    globalThis.fetch = vi.fn(async (url: string) => {
      if (String(url).endsWith('/auth/status')) {
        statusCalls++;
        return new Response(
          JSON.stringify(
            statusCalls === 1
              ? { authenticated: false }
              : { authenticated: true, host: 'github.com', login: 'octocat', name: 'Mona' },
          ),
          { status: 200 },
        );
      }
      return severedAfterVerification();
    }) as unknown as typeof fetch;

    const events = await collectEvents(httpAuthTransport().start() as never);
    expect(events.map((e) => e.type)).toEqual(['verification', 'complete']);
    expect(events[1]?.login).toBe('octocat');
    expect(events[1]?.name).toBe('Mona');
    expect(statusCalls).toBeGreaterThanOrEqual(2);
  }, 20_000);

  test('a clean stream end after the code was issued also recovers rather than failing', async () => {
    globalThis.fetch = vi.fn(async (url: string) => {
      if (String(url).endsWith('/auth/status')) {
        return new Response(
          JSON.stringify({ authenticated: true, host: 'github.com', login: 'octocat' }),
          { status: 200 },
        );
      }
      return ndjsonResponse([VERIFICATION]);
    }) as unknown as typeof fetch;

    const events = await collectEvents(httpAuthTransport().start() as never);
    expect(events.map((e) => e.type)).toEqual(['verification', 'complete']);
    expect(events[1]?.login).toBe('octocat');
  }, 20_000);

  test('an expired code ends recovery with the expiry error, not a stream error', async () => {
    globalThis.fetch = vi.fn(async (url: string) => {
      if (String(url).endsWith('/auth/status')) {
        return new Response(JSON.stringify({ authenticated: false }), { status: 200 });
      }
      return ndjsonResponse([
        JSON.stringify({
          type: 'verification',
          user_code: 'AB-12',
          verification_uri: 'https://github.com/login/device',
          expires_in: 0,
        }),
      ]);
    }) as unknown as typeof fetch;

    const events = await collectEvents(httpAuthTransport().start() as never);
    expect(events.map((e) => e.type)).toEqual(['verification', 'error']);
    expect(events[1]?.message).toContain('expired');
  });

  test('user cancel stops recovery and tells the server it was intentional', async () => {
    const cancelCalls: unknown[] = [];
    globalThis.fetch = vi.fn(async (url: string, init?: RequestInit) => {
      if (String(url).endsWith('/auth/cancel')) {
        cancelCalls.push(JSON.parse(String(init?.body)));
        return new Response('{}', { status: 200 });
      }
      if (String(url).endsWith('/auth/status')) {
        return new Response(JSON.stringify({ authenticated: false }), { status: 200 });
      }
      return ndjsonResponse([VERIFICATION]);
    }) as unknown as typeof fetch;

    const handle = httpAuthTransport().start();
    const iter = handle.events[Symbol.asyncIterator]();
    const first = await iter.next();
    expect((first.value as { type: string }).type).toBe('verification');

    handle.cancel();

    expect((await iter.next()).done).toBe(true);
    expect(cancelCalls).toEqual([{ channel: 'login' }]);
  });

  test('cancel after a completed flow does not tell the server to cancel', async () => {
    const cancelCalls: unknown[] = [];
    globalThis.fetch = vi.fn(async (url: string) => {
      if (String(url).endsWith('/auth/cancel')) {
        cancelCalls.push(url);
        return new Response('{}', { status: 200 });
      }
      return ndjsonResponse([
        VERIFICATION,
        JSON.stringify({ type: 'complete', host: 'github.com', login: 'octocat' }),
      ]);
    }) as unknown as typeof fetch;

    const handle = httpAuthTransport().start();
    await collectEvents(handle as never);
    handle.cancel();
    expect(cancelCalls).toEqual([]);
  });

  test('the gh-login flow cancels its own channel, not the device-flow slot', async () => {
    const cancelBodies: unknown[] = [];
    globalThis.fetch = vi.fn(async (url: string, init?: RequestInit) => {
      if (String(url).endsWith('/auth/cancel')) {
        cancelBodies.push(JSON.parse(String(init?.body)));
        return new Response('{}', { status: 200 });
      }
      if (String(url).endsWith('/auth/status')) {
        return new Response(JSON.stringify({ authenticated: false }), { status: 200 });
      }
      return ndjsonResponse([VERIFICATION]);
    }) as unknown as typeof fetch;

    const handle = httpAuthTransport().ghLogin?.('ghes.acme.test') as never as {
      events: AsyncIterable<unknown>;
      cancel: () => void;
    };
    const iter = handle.events[Symbol.asyncIterator]();
    await iter.next();
    handle.cancel();
    expect(cancelBodies).toEqual([{ channel: 'gh-login' }]);
  });
});