diff --git a/src/__tests__/client.test.ts b/src/__tests__/client.test.ts index 52760522..11749263 100644 --- a/src/__tests__/client.test.ts +++ b/src/__tests__/client.test.ts @@ -167,6 +167,27 @@ describe('Pushy server config', () => { ); }); + test('errors and invalid-update reports go through the client report', async () => { + setupClientMocks(); + const { client } = await createClient(); + const report = spyOn(client as any, 'report').mockImplementation( + async () => {} + ); + + (client as any).emitError(new Error('offline'), 'errorChecking'); + client.reportInvalidUpdateOnce('noArtifact', 'h2'); + + expect(report).toHaveBeenCalledWith( + expect.objectContaining({ type: 'errorChecking', message: 'offline' }) + ); + expect(report).toHaveBeenCalledWith( + expect.objectContaining({ + type: 'errorUpdate', + data: { newVersion: 'h2' }, + }) + ); + }); + test('calls afterCheckUpdate with skipped when beforeCheckUpdate returns false', async () => { setupClientMocks(); const beforeCheckUpdate = mock(() => false); diff --git a/src/client.ts b/src/client.ts index 8b620266..d219780d 100644 --- a/src/client.ts +++ b/src/client.ts @@ -80,6 +80,7 @@ export class Pushy { private readonly reporter = new Reporter({ options: this.options, getEffectivePackageVersion: () => this.getEffectivePackageVersion(), + report: (event) => this.report(event), telemetryEndpoint: () => this.checker.lastWorkingEndpoint || this.options.server?.main?.[0], }); diff --git a/src/clientReporter.ts b/src/clientReporter.ts index 38920607..1b1a0e90 100644 --- a/src/clientReporter.ts +++ b/src/clientReporter.ts @@ -43,6 +43,11 @@ export type UpdateErrorListener = ( export interface ReporterHost { readonly options: ClientOptions; getEffectivePackageVersion(): string; + /** + * The client's report: events raised here go through it, so a caller that + * replaces client.report sees them too. + */ + report(event: ReportEvent): Promise; /** The endpoint telemetry posts to: the last one that served a check. */ telemetryEndpoint(): string | undefined; } @@ -91,7 +96,7 @@ export class Reporter { return; } this.reportedInvalidUpdates.add(key); - this.report({ + this.host.report({ type: 'errorUpdate', message: reason === 'missingHash' @@ -336,7 +341,7 @@ export class Reporter { }: { message?: string; data?: Record } = {} ) => { this.emittedErrors.add(error); - this.report({ + this.host.report({ type, message, code: error.code,