Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

1.3 KB
Raw Blame
// Stop an unmocked happy-dom fetch before it reaches its localhost test origin.
// A caller may catch the rejection as an ordinary offline state, so the test
// runner must also check takeError() after each test.
export function createUnexpectedFetchGuard(testName) {
  const requests = new Map();

  const guardedFetch = (input) => {
    let path = '<invalid URL>';
    try {
      const target = typeof input === 'string' || input instanceof URL ? input : input?.url;
      path = new URL(target, 'http://test.invalid').pathname;
    } catch {
      // Keep malformed input out of reports as well.
    }
    const owner = testName() || '<outside a test>';
    const key = `${owner}: ${path}`;
    requests.set(key, (requests.get(key) || 0) + 1);
    return Promise.reject(new Error(`Unexpected test fetch in ${key}`));
  };

  const takeError = () => {
    if (requests.size === 0) return null;
    const entries = [...requests];
    requests.clear();
    const first = entries.slice(0, 5).map(([key, count]) => `${key} (${count}x)`);
    const remaining = entries.length - first.length;
    return new Error(`Unexpected test fetch; mock this request or use an owned fixture: ${first.join(', ')}${remaining ? `, and ${remaining} more` : ''}`);
  };

  return { fetch: guardedFetch, takeError };
}