|
| 1 | +import { |
| 2 | + AbstractRemote, |
| 3 | + AbstractRemoteOptions, |
| 4 | + BSONImplementation, |
| 5 | + DEFAULT_REMOTE_LOGGER, |
| 6 | + FetchImplementation, |
| 7 | + FetchImplementationProvider, |
| 8 | + ILogger, |
| 9 | + RemoteConnector |
| 10 | +} from '@powersync/common'; |
| 11 | + |
| 12 | +/** |
| 13 | + * Mock WebRemote that throws 401 Unauthorized errors for all HTTP requests. |
| 14 | + * Used for testing error handling in the shared sync worker. |
| 15 | + * Other tests may override this for managed streams. |
| 16 | + */ |
| 17 | +class MockFetchProvider extends FetchImplementationProvider { |
| 18 | + getFetch(): FetchImplementation { |
| 19 | + // Return a mock fetch that always returns 401 |
| 20 | + return async (input: RequestInfo | URL, init?: RequestInit): Promise<Response> => { |
| 21 | + const response = new Response(null, { |
| 22 | + status: 401, |
| 23 | + statusText: 'Unauthorized' |
| 24 | + }); |
| 25 | + return response; |
| 26 | + }; |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +export class WebRemote extends AbstractRemote { |
| 31 | + private _bson: BSONImplementation | undefined; |
| 32 | + |
| 33 | + constructor( |
| 34 | + protected connector: RemoteConnector, |
| 35 | + protected logger: ILogger = DEFAULT_REMOTE_LOGGER, |
| 36 | + options?: Partial<AbstractRemoteOptions> |
| 37 | + ) { |
| 38 | + super(connector, logger, { |
| 39 | + ...(options ?? {}), |
| 40 | + fetchImplementation: options?.fetchImplementation ?? new MockFetchProvider() |
| 41 | + }); |
| 42 | + } |
| 43 | + |
| 44 | + getUserAgent(): string { |
| 45 | + return 'powersync-web-mock'; |
| 46 | + } |
| 47 | + |
| 48 | + async getBSON(): Promise<BSONImplementation> { |
| 49 | + if (this._bson) { |
| 50 | + return this._bson; |
| 51 | + } |
| 52 | + const { BSON } = await import('bson'); |
| 53 | + this._bson = BSON; |
| 54 | + return this._bson; |
| 55 | + } |
| 56 | +} |
0 commit comments