Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

25.1 KB
Raw Blame
/**
 * ServiceKeyService: when an installation registers, what it sends, how it
 * keeps, renews and gives up its key, and that nothing of it ever makes a
 * request wait. The store and the issuer are fakes; the clock, the proof of
 * work and the key format are the real ones.
 */
import { runningVersion } from '../../../../src/app-config';
import { RuntimeEnvService } from '../../../../src/nest/app-config/runtime-env.service';
import { servicesConfig } from '../../../../src/nest/app-config/tokens';
import { DomainError } from '../../../../src/nest/common/domain-error';
import { IssuerClient, IssuerError } from '../../../../src/nest/service-identity/issuer.client';
import type {
  DialectInputs,
  RegistrationDialect,
} from '../../../../src/nest/service-identity/registration-dialect.types';
import { ServiceClock } from '../../../../src/nest/service-identity/service-clock';
import { REGISTRATION_DIALECT } from '../../../../src/nest/service-identity/service-identity.constants';
import { keyFingerprint } from '../../../../src/nest/service-identity/service-identity.helpers';
import { ServiceKeyService } from '../../../../src/nest/service-identity/service-key.service';
import {
  EMPTY_KEY_STATE,
  ServiceKeyStore,
  type KeyState,
  type StoredIdentity,
} from '../../../../src/nest/service-identity/service-key.store';
import { createSnapshotTestDb } from '../../../helpers/db-mock';
import { oneByOne } from '../../../helpers/one-by-one';
import { serviceKeyTokenFor } from '../../../helpers/service-keys';
import { createTestModule, type TestModule } from '../../../helpers/test-module';

import { afterAll, afterEach, describe, expect, it, vi } from 'vitest';

const { logWarn } = vi.hoisted(() => ({ logWarn: vi.fn() }));
vi.mock('../../../../src/nest/audit/audit-log.logger', async (importOriginal) => ({
  ...(await importOriginal<typeof import('../../../../src/nest/audit/audit-log.logger')>()),
  logWarn,
}));

const DAY = 86_400;
const now = () => Math.floor(Date.now() / 1000);

const KEY = serviceKeyTokenFor({ i: 'installation-a', iat: now(), exp: now() + 90 * DAY });
const OTHER_KEY = serviceKeyTokenFor({ i: 'installation-b', iat: now(), exp: now() + 90 * DAY });
const RENEWED_KEY = serviceKeyTokenFor({ i: 'installation-a', iat: now(), exp: now() + 180 * DAY });

function fakeStore(initial: Partial<StoredIdentity> = {}) {
  let stored: StoredIdentity = { key: null, unreadable: false, state: EMPTY_KEY_STATE, ...initial };
  let raced: StoredIdentity['key'] = null;
  return {
    current: () => stored,
    /** The next claim finds this key already stored by another process. */
    raceWith: (key: StoredIdentity['key']) => {
      raced = key;
    },
    read: vi.fn(async () => stored),
    claim: vi.fn(async (key: NonNullable<StoredIdentity['key']>, state: KeyState) => {
      if (raced) stored = { key: raced, unreadable: false, state };
      else if (!stored.key && !stored.unreadable) stored = { key, unreadable: false, state };
      return stored;
    }),
    replace: vi.fn(async (key: NonNullable<StoredIdentity['key']>, state: KeyState) => {
      stored = { key, unreadable: false, state };
    }),
    writeState: vi.fn(async (state: KeyState) => {
      stored = { ...stored, state };
    }),
    dropKey: vi.fn(async (state: KeyState) => {
      stored = { key: null, unreadable: false, state };
    }),
    clear: vi.fn(async () => {
      stored = { key: null, unreadable: false, state: EMPTY_KEY_STATE };
    }),
  };
}

function fakeIssuer(issued = KEY) {
  const serverMs = () => Date.now();
  return {
    challenge: vi.fn(async () => ({
      body: { c: 'v1.challenge', alg: 'scrypt' as const, N: 2, r: 1, p: 1, bits: 0, expires_at: now() + 600 },
      serverMs: serverMs(),
    })),
    register: vi.fn(async () => ({
      body: { key: issued, exp: now() + 90 * DAY, renew_after: now() + 60 * DAY },
      serverMs: serverMs(),
    })),
    renew: vi.fn(async () => ({
      body: { key: RENEWED_KEY, exp: now() + 180 * DAY, renew_after: now() + 150 * DAY },
      serverMs: serverMs(),
    })),
    quota: vi.fn(),
  };
}

/**
 * The original registration, pinned here rather than read from the committed
 * dialect, which every release replaces; registration-dialect.test.ts holds
 * whatever the committed one is to the issuer's own computation.
 */
const DIALECT_ZERO: RegistrationDialect = {
  id: '0',
  releasedFor: 'dev',
  createdAt: '2026-10-29',
  challengePath: '/v1/register/challenge',
  registerPath: '/v1/register',
  begin: ({ challenge, version }) => ({
    work: (nonce) => ({ password: Buffer.from(`${challenge.c}.${nonce}`), salt: Buffer.from('trek-pow-v1') }),
    request: (nonce) => ({ body: { c: challenge.c, nonce: String(nonce), v: version }, headers: {} }),
  }),
};

const testDb = createSnapshotTestDb();
let t: TestModule | null = null;

interface Setup {
  config?: Partial<{ mode: 'auto' | 'off'; key: string | null; hosts: string[] }>;
  places?: { enabled?: boolean; url?: string };
  isTest?: boolean;
  store?: ReturnType<typeof fakeStore>;
  issuer?: ReturnType<typeof fakeIssuer>;
  dialect?: RegistrationDialect;
}

async function build(setup: Setup = {}) {
  const store = setup.store ?? fakeStore();
  const issuer = setup.issuer ?? fakeIssuer();
  const config = { mode: 'auto' as const, key: null, hosts: ['places.liketrek.com'], ...setup.config };
  const maps = { trekPlacesEnabled: setup.places?.enabled ?? true, trekPlacesUrl: setup.places?.url };
  t = await createTestModule({
    db: testDb,
    imports: [],
    providers: [
      ServiceKeyService,
      ServiceClock,
      { provide: ServiceKeyStore, useValue: store },
      { provide: IssuerClient, useValue: issuer },
      { provide: servicesConfig.KEY, useValue: config },
      { provide: RuntimeEnvService, useValue: { env: () => ({ maps }), isTest: () => setup.isTest ?? false } },
      { provide: REGISTRATION_DIALECT, useValue: setup.dialect ?? DIALECT_ZERO },
    ],
  });
  return { keys: t.get(ServiceKeyService), store, issuer };
}

/** Lets the background work of the service run to its end. */
const settled = (keys: ServiceKeyService) => keys.whenIdle();

afterEach(async () => {
  vi.useRealTimers();
  logWarn.mockClear();
  await t?.close();
  t = null;
});

afterAll(() => testDb.close());

describe('registration', () => {
  it('SIKEYS-001: the first request goes out without a key, and the installation registers in the background', async () => {
    const { keys, store, issuer } = await build();
    expect(keys.bearer()).toBeNull();
    await settled(keys);
    expect(issuer.challenge).toHaveBeenCalledWith('/v1/register/challenge');
    expect(issuer.register).toHaveBeenCalledWith('/v1/register', {
      body: { c: 'v1.challenge', nonce: '0', v: runningVersion().slice(0, 32) },
      headers: {},
    });
    expect(store.claim).toHaveBeenCalledTimes(1);
    expect(store.current().state.renewAfter).toBeGreaterThan(now());
    expect(keys.bearer()).toBe(KEY);
  });

  it('SIKEYS-002: registers once however many requests ask at the same time', async () => {
    const { keys, issuer } = await build();
    for (let i = 0; i < 5; i++) keys.bearer();
    await settled(keys);
    keys.bearer();
    await settled(keys);
    expect(issuer.challenge).toHaveBeenCalledTimes(1);
  });

  it('SIKEYS-003: keeps the key another process stored first', async () => {
    const store = fakeStore();
    const { keys } = await build({ store });
    store.raceWith({
      token: OTHER_KEY,
      kid: 'test',
      payload: { i: 'installation-b', iat: now(), exp: now() + DAY, q: 'std' },
    });
    keys.bearer();
    await settled(keys);
    expect(keys.bearer()).toBe(OTHER_KEY);
  });

  it('SIKEYS-004: registers anew without a word when the stored key cannot be read', async () => {
    const store = fakeStore({ unreadable: true });
    const { keys, issuer } = await build({ store });
    keys.bearer();
    await settled(keys);
    expect(issuer.register).toHaveBeenCalledTimes(1);
    expect(store.replace).toHaveBeenCalledTimes(1);
    expect(store.claim).not.toHaveBeenCalled();
    expect(keys.bearer()).toBe(KEY);
  });

  it('SIKEYS-005: a compose line that was only uncommented still counts as the public service', async () => {
    const { keys, issuer } = await build({ places: { url: 'HTTPS://Places.LikeTrek.com/' } });
    keys.bearer();
    await settled(keys);
    expect(issuer.register).toHaveBeenCalledTimes(1);
  });

  it('SIKEYS-006: backs off after a refusal, as long as the issuer asks', async () => {
    vi.useFakeTimers({ toFake: ['Date'] });
    const issuer = fakeIssuer();
    issuer.challenge.mockRejectedValueOnce(new IssuerError(429, 60_000, null));
    const { keys } = await build({ issuer });
    keys.bearer();
    await settled(keys);
    keys.bearer();
    await settled(keys);
    expect(issuer.challenge).toHaveBeenCalledTimes(1);
    vi.setSystemTime(Date.now() + 61_000);
    keys.bearer();
    await settled(keys);
    expect(issuer.challenge).toHaveBeenCalledTimes(2);
    expect(keys.bearer()).toBe(KEY);
  });

  it('SIKEYS-007: a refused challenge is fetched once more at once, then left to the backoff', async () => {
    const issuer = fakeIssuer();
    issuer.register.mockRejectedValueOnce(new IssuerError(400, null, null));
    const { keys } = await build({ issuer });
    keys.bearer();
    await settled(keys);
    expect(issuer.challenge).toHaveBeenCalledTimes(2);
    expect(keys.bearer()).toBe(KEY);

    const refusing = fakeIssuer();
    refusing.register.mockRejectedValue(new IssuerError(400, null, null));
    await t?.close();
    const again = await build({ issuer: refusing });
    again.keys.bearer();
    await settled(again.keys);
    again.keys.bearer();
    await settled(again.keys);
    expect(refusing.challenge).toHaveBeenCalledTimes(2);
  });

  it('SIKEYS-008: a release whose dialect the issuer no longer takes waits a day, says so once, and is not retried', async () => {
    vi.useFakeTimers({ toFake: ['Date'] });
    const issuer = fakeIssuer();
    issuer.challenge.mockRejectedValue(new IssuerError(410, null, null));
    const { keys, store } = await build({ issuer });
    keys.bearer();
    await settled(keys);
    expect(issuer.challenge).toHaveBeenCalledTimes(1);
    keys.bearer();
    await settled(keys);
    expect(issuer.challenge).toHaveBeenCalledTimes(1);

    vi.setSystemTime(Date.now() + 23 * 3_600_000);
    keys.bearer();
    await settled(keys);
    expect(issuer.challenge).toHaveBeenCalledTimes(1);
    vi.setSystemTime(Date.now() + 2 * 3_600_000);
    keys.bearer();
    await settled(keys);
    expect(issuer.challenge).toHaveBeenCalledTimes(2);

    const lines = logWarn.mock.calls.map(([line]) => String(line));
    expect(lines).toHaveLength(1);
    expect(lines[0]).toMatch(/can no longer register/);
    expect(store.claim).not.toHaveBeenCalled();
    expect(keys.bearer()).toBeNull();
    expect((await keys.snapshot()).state).toBe('legacy');
  });

  it('SIKEYS-015: the wait after a refused dialect follows the issuer when it names one', async () => {
    vi.useFakeTimers({ toFake: ['Date'] });
    const issuer = fakeIssuer();
    issuer.register.mockRejectedValueOnce(new IssuerError(410, 3_600_000, null));
    const { keys } = await build({ issuer });
    keys.bearer();
    await settled(keys);
    expect(issuer.challenge).toHaveBeenCalledTimes(1);
    vi.setSystemTime(Date.now() + 3_601_000);
    keys.bearer();
    await settled(keys);
    expect(issuer.challenge).toHaveBeenCalledTimes(2);
    expect(keys.bearer()).toBe(KEY);
  });

  it('SIKEYS-009: the dialect decides where to ask, what to send and over what the work runs', async () => {
    const seen: DialectInputs[] = [];
    const worked: number[] = [];
    const dialect: RegistrationDialect = {
      id: 'AbCdEfGhIjKl',
      releasedFor: '9.9.9',
      createdAt: '2026-11-01',
      challengePath: '/v1/register/AbCdEfGhIjKl/challenge',
      registerPath: '/v1/register/AbCdEfGhIjKl',
      begin(inputs) {
        seen.push(inputs);
        return {
          work(nonce) {
            worked.push(nonce);
            return { password: Buffer.from(`work-${nonce}`), salt: Buffer.from('salt') };
          },
          request: (nonce) => ({ body: { harbor: nonce.toString(16) }, headers: { 'X-TREK-Ferry-Seal': 'abc' } }),
        };
      },
    };
    // The issuer's clock runs five days ahead of this server's; the dialect gets the issuer's time.
    const issuer = fakeIssuer();
    issuer.challenge.mockResolvedValueOnce({
      body: { c: 'v1.challenge', alg: 'scrypt', N: 2, r: 1, p: 1, bits: 0, expires_at: now() + 5 * DAY + 600 },
      serverMs: Date.now() + 5 * DAY * 1000,
    });
    const { keys } = await build({ dialect, issuer });
    keys.bearer();
    await settled(keys);
    expect(issuer.challenge).toHaveBeenCalledWith('/v1/register/AbCdEfGhIjKl/challenge');
    expect(issuer.register).toHaveBeenCalledWith('/v1/register/AbCdEfGhIjKl', {
      body: { harbor: '0' },
      headers: { 'X-TREK-Ferry-Seal': 'abc' },
    });
    expect(worked).toEqual([0]);
    expect(seen).toHaveLength(1);
    expect(seen[0].challenge.c).toBe('v1.challenge');
    expect(seen[0].version).toBe(runningVersion().slice(0, 32));
    expect(seen[0].random).toHaveLength(16);
    expect(Math.abs(seen[0].nowSeconds - (now() + 5 * DAY))).toBeLessThanOrEqual(2);
  });
});

describe('the check after startup and every day', () => {
  it('SIKEYS-039: registers without any request having asked first', async () => {
    const { keys, store, issuer } = await build();
    await keys.settle();
    expect(issuer.register).toHaveBeenCalledTimes(1);
    expect(store.current().key?.token).toBe(KEY);
    expect(keys.bearer()).toBe(KEY);
  });

  it('SIKEYS-040: waits out the backoff of a failed attempt, then registers on the next check', async () => {
    vi.useFakeTimers({ toFake: ['Date'] });
    const issuer = fakeIssuer();
    issuer.challenge.mockRejectedValueOnce(new Error('fetch failed'));
    const { keys } = await build({ issuer });
    await expect(keys.settle()).rejects.toThrow('fetch failed');
    await keys.settle();
    expect(issuer.challenge).toHaveBeenCalledTimes(1);
    vi.setSystemTime(Date.now() + 5 * 60_000);
    await keys.settle();
    expect(issuer.register).toHaveBeenCalledTimes(1);
    expect(keys.bearer()).toBe(KEY);
  });

  it.each([
    ['the services are off', { config: { mode: 'off' as const } }],
    ['the TREK index is off', { places: { enabled: false } }],
    ['a key comes from the environment', { config: { key: KEY } }],
    ['the index is an own copy', { places: { url: 'https://places.example.org' } }],
  ])('SIKEYS-041: does not register when %s', async (_case, setup) => {
    const { keys, issuer } = await build(setup);
    await keys.settle();
    expect(issuer.challenge).not.toHaveBeenCalled();
  });
});

describe('when it never registers', () => {
  it('SIKEYS-010: never under NODE_ENV=test', async () => {
    const { keys, issuer } = await build({ isTest: true });
    expect(keys.bearer()).toBeNull();
    await settled(keys);
    expect(issuer.challenge).not.toHaveBeenCalled();
    expect((await keys.snapshot()).state).toBe('legacy');
  });

  it.each<[string, Setup]>([
    ['the index switched off', { places: { enabled: false } }],
    ['TREK_SERVICES=off', { config: { mode: 'off' } }],
  ])('SIKEYS-011: never with %s', async (_, setup) => {
    const { keys, issuer } = await build(setup);
    expect(keys.bearer()).toBeNull();
    expect((await keys.snapshot()).state).toBe('off');
    expect(issuer.challenge).not.toHaveBeenCalled();
  });

  it('SIKEYS-012: never for a copy of the index the operator runs', async () => {
    const { keys, issuer } = await build({ places: { url: 'https://places.example.org' } });
    expect(keys.bearer()).toBeNull();
    expect((await keys.snapshot()).state).toBe('off');
    expect(issuer.challenge).not.toHaveBeenCalled();
  });

  it('SIKEYS-013: never for an allowlisted mirror, which gets the stored key but is no issuer', async () => {
    const store = fakeStore();
    const { keys, issuer } = await build({
      store,
      places: { url: 'https://mirror.example.org' },
      config: { hosts: ['places.liketrek.com', 'mirror.example.org'] },
    });
    keys.bearer();
    await settled(keys);
    expect(issuer.challenge).not.toHaveBeenCalled();
    expect((await keys.snapshot()).state).toBe('legacy');
  });

  it('SIKEYS-014: TREK_SERVICES_KEY wins over the stored key and turns registration off', async () => {
    const store = fakeStore({
      key: { token: OTHER_KEY, kid: 'test', payload: { i: 'b', iat: 1, exp: now() + DAY, q: 'std' } },
    });
    const { keys, issuer } = await build({ store, config: { key: KEY } });
    expect(keys.bearer()).toBe(KEY);
    await settled(keys);
    expect(issuer.challenge).not.toHaveBeenCalled();
    expect(await keys.snapshot()).toMatchObject({ state: 'connected', source: 'env', bearer: KEY });
  });
});

describe('refusals', () => {
  async function registered(setup: Setup = {}) {
    const built = await build(setup);
    built.keys.bearer();
    await settled(built.keys);
    expect(built.keys.bearer()).toBe(KEY);
    return built;
  }

  const tenMinutesLater = () => vi.setSystemTime(Date.now() + 10 * 60_000);

  it('SIKEYS-020: gives the key up only on the third 401 in a row, and asks again within six hours', async () => {
    vi.useFakeTimers({ toFake: ['Date'] });
    const { keys, store, issuer } = await registered();
    keys.noteInvalid();
    tenMinutesLater();
    keys.noteInvalid();
    keys.noteAccepted();
    tenMinutesLater();
    keys.noteInvalid();
    tenMinutesLater();
    keys.noteInvalid();
    expect(store.dropKey).not.toHaveBeenCalled();
    tenMinutesLater();
    const before = Date.now();
    keys.noteInvalid();
    await settled(keys);
    expect(store.dropKey).toHaveBeenCalledTimes(1);
    const reregisterAt = store.current().state.reregisterAt ?? 0;
    expect(reregisterAt).toBeGreaterThanOrEqual(before);
    expect(reregisterAt).toBeLessThanOrEqual(Date.now() + 6 * 3_600_000);
    expect(keys.bearer()).toBeNull();
    await settled(keys);
    expect(issuer.challenge).toHaveBeenCalledTimes(Date.now() >= reregisterAt ? 2 : 1);
  });

  it('SIKEYS-021: a 401 without a key changes nothing', async () => {
    const { keys, store } = await build({ isTest: true });
    for (let i = 0; i < 5; i++) keys.noteInvalid();
    await settled(keys);
    expect(store.dropKey).not.toHaveBeenCalled();
  });

  it('SIKEYS-022: a revoked key stops going out and stays given up until an admin starts over', async () => {
    const { keys, store, issuer } = await registered();
    keys.noteRevoked();
    await settled(keys);
    expect(store.current().state.revoked).toBe(keyFingerprint(KEY));
    expect(keys.bearer()).toBeNull();
    await settled(keys);
    expect(issuer.challenge).toHaveBeenCalledTimes(1);
    expect((await keys.snapshot()).state).toBe('revoked');

    issuer.register.mockResolvedValueOnce({
      body: { key: OTHER_KEY, exp: now() + 90 * DAY, renew_after: now() + 60 * DAY },
      serverMs: Date.now(),
    });
    await keys.regenerate();
    await settled(keys);
    expect(store.clear).toHaveBeenCalledTimes(1);
    expect(keys.bearer()).toBe(OTHER_KEY);
  });

  it('SIKEYS-023: an admin cannot start over a key that comes from the environment, nor with the services off', async () => {
    const env = await build({ config: { key: KEY } });
    await expect(env.keys.regenerate()).rejects.toBeInstanceOf(DomainError);
    await t?.close();
    const off = await build({ config: { mode: 'off' } });
    await expect(off.keys.regenerate()).rejects.toMatchObject({ status: 409 });
  });

  it('SIKEYS-024: the refusals of one page full of searches count once', async () => {
    vi.useFakeTimers({ toFake: ['Date'] });
    const { keys, store } = await registered();
    for (let i = 0; i < 6; i++) keys.noteInvalid();
    tenMinutesLater();
    for (let i = 0; i < 6; i++) keys.noteInvalid();
    await settled(keys);
    expect(store.dropKey).not.toHaveBeenCalled();
    expect(keys.bearer()).toBe(KEY);
  });
});

describe('renewal', () => {
  const oldKey = serviceKeyTokenFor({ i: 'installation-a', iat: now() - 61 * DAY, exp: now() + 29 * DAY });
  const stored = (state: Partial<KeyState> = {}) =>
    fakeStore({
      key: {
        token: oldKey,
        kid: 'test',
        payload: { i: 'installation-a', iat: now() - 61 * DAY, exp: now() + 29 * DAY, q: 'std' },
      },
      state: { ...EMPTY_KEY_STATE, ...state },
    });

  it('SIKEYS-030: leaves a key alone before it is due', async () => {
    const { keys, issuer } = await build({ store: stored({ renewAfter: now() + DAY }) });
    await keys.settle();
    expect(issuer.renew).not.toHaveBeenCalled();
  });

  it('SIKEYS-031: renews from day 60 with the old key, keeping the same installation', async () => {
    const store = stored();
    const { keys, issuer } = await build({ store });
    await keys.settle();
    expect(issuer.renew).toHaveBeenCalledWith(oldKey);
    expect(store.current().key?.token).toBe(RENEWED_KEY);
    expect(store.current().state.renewAfter).toBeGreaterThan(now() + 149 * DAY);
    expect(keys.bearer()).toBe(RENEWED_KEY);
  });

  it('SIKEYS-032: reads due-ness on the issuer clock, not on a server clock that is behind', async () => {
    const store = stored({ renewAfter: now() + DAY });
    const issuer = fakeIssuer();
    const { keys } = await build({ store, issuer });
    t?.get(ServiceClock).learn(Date.now() + 2 * DAY * 1000);
    await keys.settle();
    expect(issuer.renew).toHaveBeenCalledTimes(1);
  });

  it('SIKEYS-033: a refused renewal counts like a refused search, a revoked one freezes it, a network failure changes nothing', async () => {
    vi.useFakeTimers({ toFake: ['Date'] });
    const refused = fakeIssuer();
    refused.renew.mockRejectedValue(new IssuerError(401, null, null));
    const first = await build({ store: stored(), issuer: refused });
    await first.keys.settle();
    await first.keys.settle();
    expect(first.store.dropKey).not.toHaveBeenCalled();
    await oneByOne(Array.from({ length: 2 }), () => {
      vi.setSystemTime(Date.now() + 10 * 60_000);
      return first.keys.settle();
    });
    expect(first.store.dropKey).toHaveBeenCalledTimes(1);
    vi.useRealTimers();
    await t?.close();

    const revoked = fakeIssuer();
    revoked.renew.mockRejectedValueOnce(new IssuerError(403, null, null));
    const second = await build({ store: stored(), issuer: revoked });
    await second.keys.settle();
    expect(second.store.current().state.revoked).toBe(keyFingerprint(oldKey));
    await t?.close();

    const down = fakeIssuer();
    down.renew.mockRejectedValueOnce(new Error('fetch failed'));
    const third = await build({ store: stored(), issuer: down });
    await expect(third.keys.settle()).rejects.toThrow('fetch failed');
    expect(third.store.current().key?.token).toBe(oldKey);
  });

  it('SIKEYS-034: a key past its year of grace registers anew instead', async () => {
    const lapsed = serviceKeyTokenFor({ i: 'installation-a', iat: now() - 456 * DAY, exp: now() - 366 * DAY });
    const store = fakeStore({
      key: {
        token: lapsed,
        kid: 'test',
        payload: { i: 'installation-a', iat: now() - 456 * DAY, exp: now() - 366 * DAY, q: 'std' },
      },
    });
    const { keys, issuer } = await build({ store });
    await keys.settle();
    expect(issuer.renew).not.toHaveBeenCalled();
    keys.bearer();
    await settled(keys);
    expect(store.replace).toHaveBeenCalledTimes(1);
    expect(keys.bearer()).toBe(KEY);
  });

  it('SIKEYS-035: never renews a key from the environment', async () => {
    const { keys, issuer } = await build({ store: stored(), config: { key: KEY } });
    await keys.settle();
    expect(issuer.renew).not.toHaveBeenCalled();
  });

  it('SIKEYS-036: a due key is renewed on the next request, not only by the daily check', async () => {
    const store = stored();
    const { keys, issuer } = await build({ store });
    await keys.snapshot();
    expect(keys.bearer()).toBe(oldKey);
    await settled(keys);
    expect(issuer.renew).toHaveBeenCalledWith(oldKey);
    expect(keys.bearer()).toBe(RENEWED_KEY);
  });

  it.each([5, 300])(
    'SIKEYS-037: a key that expired %i days ago is within its grace, stays home, and the next request renews it',
    async (daysAgo) => {
      const iat = now() - (90 + daysAgo) * DAY;
      const exp = now() - daysAgo * DAY;
      const expired = serviceKeyTokenFor({ i: 'installation-a', iat, exp });
      const store = fakeStore({
        key: { token: expired, kid: 'test', payload: { i: 'installation-a', iat, exp, q: 'std' } },
      });
      const { keys, issuer } = await build({ store });
      expect(keys.bearer()).toBeNull();
      await settled(keys);
      expect(issuer.renew).toHaveBeenCalledWith(expired);
      expect(issuer.register).not.toHaveBeenCalled();
      expect(keys.bearer()).toBe(RENEWED_KEY);
    },
  );

  it('SIKEYS-038: after a failed renewal the requests wait for the backoff instead of asking again', async () => {
    vi.useFakeTimers({ toFake: ['Date'] });
    const issuer = fakeIssuer();
    issuer.renew.mockRejectedValueOnce(new Error('fetch failed'));
    const { keys } = await build({ store: stored(), issuer });
    keys.bearer();
    await settled(keys);
    keys.bearer();
    await settled(keys);
    expect(issuer.renew).toHaveBeenCalledTimes(1);
    vi.setSystemTime(Date.now() + 5 * 60_000);
    keys.bearer();
    await settled(keys);
    expect(issuer.renew).toHaveBeenCalledTimes(2);
    expect(keys.bearer()).toBe(RENEWED_KEY);
  });
});