Skip to content

Commit 7928490

Browse files
committed
feat(streams): use logger
1 parent 5c79eed commit 7928490

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed

packages/streams/src/browser/ChromeRuntimeStream.test.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,21 @@ const makeEnvelope = (
3131

3232
const EXTENSION_ID = 'test-extension-id';
3333

34+
const mocks = vi.hoisted(() => ({
35+
logger: {
36+
debug: vi.fn(),
37+
warn: vi.fn(),
38+
},
39+
}));
40+
41+
vi.mock('@ocap/utils', async (importOriginal) => {
42+
const actual = await importOriginal<typeof import('@ocap/utils')>();
43+
return {
44+
...actual,
45+
makeLogger: vi.fn(() => mocks.logger),
46+
};
47+
});
48+
3449
// This function declares its own return type.
3550
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
3651
const makeRuntime = (extensionId: string = EXTENSION_ID) => {
@@ -165,11 +180,11 @@ describe('ChromeRuntimeReader', () => {
165180

166181
const nextP = reader.next();
167182

168-
vi.spyOn(console, 'debug');
183+
const debugSpy = vi.spyOn(mocks.logger, 'debug');
169184
listeners[0]?.({ not: 'an envelope' }, { id: EXTENSION_ID });
170185

171-
expect(console.debug).toHaveBeenCalledWith(
172-
`ChromeRuntimeReader received unexpected message: ${stringify({
186+
expect(debugSpy).toHaveBeenCalledWith(
187+
`received unexpected message: ${stringify({
173188
not: 'an envelope',
174189
})}`,
175190
);
@@ -189,7 +204,7 @@ describe('ChromeRuntimeReader', () => {
189204

190205
const nextP = reader.next();
191206

192-
vi.spyOn(console, 'warn');
207+
vi.spyOn(mocks.logger, 'warn');
193208
const message1 = { foo: 'bar' };
194209
dispatchRuntimeMessage(
195210
message1,

packages/streams/src/browser/ChromeRuntimeStream.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
*/
1616

1717
import type { Json } from '@metamask/utils';
18-
import { stringify } from '@ocap/utils';
18+
import { makeLogger, stringify } from '@ocap/utils';
19+
import type { Logger } from '@ocap/utils';
1920

2021
import type { ChromeRuntime, ChromeMessageSender } from './chrome.d.ts';
2122
import {
@@ -67,6 +68,8 @@ export class ChromeRuntimeReader<Read extends Json> extends BaseReader<Read> {
6768

6869
readonly #extensionId: string;
6970

71+
readonly #logger: Logger;
72+
7073
constructor(
7174
runtime: ChromeRuntime,
7275
target: ChromeRuntimeTarget,
@@ -91,6 +94,7 @@ export class ChromeRuntimeReader<Read extends Json> extends BaseReader<Read> {
9194
this.#target = target;
9295
this.#source = source;
9396
this.#extensionId = runtime.id;
97+
this.#logger = makeLogger(`[chrome-runtime-reader ${this.#extensionId}]`);
9498

9599
messageListener = this.#onMessage.bind(this);
96100
// Begin listening for messages from the Chrome runtime.
@@ -105,19 +109,15 @@ export class ChromeRuntimeReader<Read extends Json> extends BaseReader<Read> {
105109
}
106110

107111
if (!isMessageEnvelope(message)) {
108-
console.debug(
109-
`ChromeRuntimeReader received unexpected message: ${stringify(
110-
message,
111-
)}`,
112-
);
112+
this.#logger.debug(`received unexpected message: ${stringify(message)}`);
113113
return;
114114
}
115115

116116
if (message.target !== this.#target || message.source !== this.#source) {
117-
console.debug(
118-
`ChromeRuntimeReader received message with incorrect target or source: ${stringify(message)}`,
119-
`Expected target: ${this.#target}`,
120-
`Expected source: ${this.#source}`,
117+
this.#logger.debug(
118+
`received message with incorrect target or source: ${stringify(message)}`,
119+
`expected target: ${this.#target}`,
120+
`expected source: ${this.#source}`,
121121
);
122122
return;
123123
}

0 commit comments

Comments
 (0)