Files
puter/src/backend/core/context.test.ts
T
Daniel Salazar 8ecfeec8c3 fix(puter-js): request idle timeout, callback/listener leaks, guarded message handlers (PUT-1908) (#4143)
* fix(puter-js): free one-shot callbacks and listeners, guard message handlers

- setAuthToken starts the GUI cache refresh loop once instead of per call
- IPC reply callbacks are dropped after their reply; dehydrated callbacks stay
- AppConnection removes its window listener when the target app closes
- reply branches for app data and instance count drop their callback
- UI, Debug, tool and GUI-token message listeners no longer reject; an
  unknown or failing tool answers toolResponse with an error
- parseResponse handles a blob response with no content-type
- retry waits remove their abort listener when the wait ends
- EmailConfirmationDialog escapes its message
- EventListener uses a real Map and snapshots listeners per emit
- Context get/set/runWithContext route every known key the same way

* fix(puter-js): time out requests that make no progress

- buildXhr starts an idle clock on send, reset by state changes and body
  progress (upload progress only on requests already carrying a bearer)
- read-safe requests get 60 s while progress is observable, everything
  else 15 min; timeout: 0 on fetchUrl/driverCall/sendWithRetry turns it off
- a timed-out read replays once; writes and AI calls never replay
- timeouts reject with code request_timeout on fetchUrl (TypeError),
  driverCall, streams and the legacy XHR path
- document the limits in rate-limits-and-quotas and the chat stream note
2026-10-09 00:07:20 -07:00

53 lines
2.0 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import { Context, runInDerivedContext, runWithContext } from './context';
describe('Context', () => {
it('reads back a known key set after the scope opened with it', () => {
runWithContext({ driverName: 'initial' }, () => {
Context.set('driverName', 'addressed');
expect(Context.get('driverName')).toBe('addressed');
});
});
it('reads every known key from where set writes it', () => {
const signal = new AbortController().signal;
runWithContext({}, () => {
Context.set('requestId', 'r-1');
Context.set('driverName', 'd');
Context.set('abortSignal', signal);
Context.set('strictUpstreamErrors', true);
expect(Context.get('requestId')).toBe('r-1');
expect(Context.get('driverName')).toBe('d');
expect(Context.get('abortSignal')).toBe(signal);
expect(Context.get('strictUpstreamErrors')).toBe(true);
expect(Context.current()?.extra.size).toBe(0);
});
});
it('keeps ad-hoc keys in the open map', () => {
runWithContext({}, () => {
Context.set('myService.txId', 'tx');
expect(Context.get('myService.txId')).toBe('tx');
expect(Context.get('toString')).toBeUndefined();
});
});
it('keeps an untyped extra key passed to the scope readable', () => {
const initial = { requestId: 'r', 'myService.txId': 'tx' };
runWithContext(initial as { requestId: string }, () => {
expect(Context.get('requestId')).toBe('r');
expect(Context.get('myService.txId')).toBe('tx');
});
});
it('isolates a derived scope from its parent', () => {
runWithContext({ driverName: 'outer' }, () => {
runInDerivedContext(() => {
Context.set('driverName', 'inner');
expect(Context.get('driverName')).toBe('inner');
});
expect(Context.get('driverName')).toBe('outer');
});
});
});