Skip to content

Commit e27dcac

Browse files
committed
[Telemetry] Use dependency injection for WatchdogClient in ClearcutLogger
1 parent ae8e9ba commit e27dcac

2 files changed

Lines changed: 25 additions & 18 deletions

File tree

src/telemetry/clearcut-logger.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,17 @@ export class ClearcutLogger {
2323
persistence?: Persistence;
2424
logFile?: string;
2525
appVersion: string;
26+
watchdogClient?: WatchdogClient;
2627
}) {
2728
this.#persistence = options.persistence ?? new FilePersistence();
28-
this.#watchdog = new WatchdogClient({
29-
parentPid: process.pid,
30-
appVersion: options.appVersion,
31-
osType: this.#detectOsType(),
32-
logFile: options.logFile,
33-
});
29+
this.#watchdog =
30+
options.watchdogClient ??
31+
new WatchdogClient({
32+
parentPid: process.pid,
33+
appVersion: options.appVersion,
34+
osType: this.#detectOsType(),
35+
logFile: options.logFile,
36+
});
3437
}
3538

3639
#detectOsType(): OsType {

tests/telemetry/clearcut-logger.test.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,15 @@ import {IpcMessageType} from '../../src/telemetry/types.js';
1717

1818
describe('ClearcutLogger', () => {
1919
let mockPersistence: sinon.SinonStubbedInstance<Persistence>;
20-
let sendStub: sinon.SinonStub;
20+
let mockWatchdogClient: sinon.SinonStubbedInstance<WatchdogClient>;
2121

2222
beforeEach(() => {
2323
mockPersistence = sinon.createStubInstance(FilePersistence, {
2424
loadState: Promise.resolve({
2525
lastActive: '',
2626
}),
2727
});
28-
// Stub the prototype of WatchdogClient so newly created instances use it
29-
sendStub = sinon.stub(WatchdogClient.prototype, 'send');
28+
mockWatchdogClient = sinon.createStubInstance(WatchdogClient);
3029
});
3130

3231
afterEach(() => {
@@ -38,15 +37,16 @@ describe('ClearcutLogger', () => {
3837
const logger = new ClearcutLogger({
3938
persistence: mockPersistence,
4039
appVersion: '1.0.0',
40+
watchdogClient: mockWatchdogClient,
4141
});
4242
await logger.logToolInvocation({
4343
toolName: 'test_tool',
4444
success: true,
4545
latencyMs: 123,
4646
});
4747

48-
assert(sendStub.calledOnce);
49-
const msg = sendStub.firstCall.args[0];
48+
assert(mockWatchdogClient.send.calledOnce);
49+
const msg = mockWatchdogClient.send.firstCall.args[0];
5050
assert.strictEqual(msg.type, IpcMessageType.EVENT);
5151
assert.strictEqual(msg.payload.tool_invocation?.tool_name, 'test_tool');
5252
assert.strictEqual(msg.payload.tool_invocation?.success, true);
@@ -59,13 +59,14 @@ describe('ClearcutLogger', () => {
5959
const logger = new ClearcutLogger({
6060
persistence: mockPersistence,
6161
appVersion: '1.0.0',
62+
watchdogClient: mockWatchdogClient,
6263
});
6364

6465
await logger.logServerStart({headless: true});
6566

6667
// Should have logged server start
67-
assert(sendStub.calledOnce);
68-
const msg = sendStub.firstCall.args[0];
68+
assert(mockWatchdogClient.send.calledOnce);
69+
const msg = mockWatchdogClient.send.firstCall.args[0];
6970
assert.strictEqual(msg.type, IpcMessageType.EVENT);
7071
assert.strictEqual(
7172
msg.payload.server_start?.flag_usage?.headless,
@@ -86,12 +87,13 @@ describe('ClearcutLogger', () => {
8687
const logger = new ClearcutLogger({
8788
persistence: mockPersistence,
8889
appVersion: '1.0.0',
90+
watchdogClient: mockWatchdogClient,
8991
});
9092

9193
await logger.logDailyActiveIfNeeded();
9294

93-
assert(sendStub.calledOnce);
94-
const msg = sendStub.firstCall.args[0];
95+
assert(mockWatchdogClient.send.calledOnce);
96+
const msg = mockWatchdogClient.send.firstCall.args[0];
9597
assert.strictEqual(msg.type, IpcMessageType.EVENT);
9698
assert.ok(msg.payload.daily_active);
9799

@@ -106,11 +108,12 @@ describe('ClearcutLogger', () => {
106108
const logger = new ClearcutLogger({
107109
persistence: mockPersistence,
108110
appVersion: '1.0.0',
111+
watchdogClient: mockWatchdogClient,
109112
});
110113

111114
await logger.logDailyActiveIfNeeded();
112115

113-
assert(sendStub.notCalled);
116+
assert(mockWatchdogClient.send.notCalled);
114117
assert(mockPersistence.saveState.notCalled);
115118
});
116119

@@ -122,12 +125,13 @@ describe('ClearcutLogger', () => {
122125
const logger = new ClearcutLogger({
123126
persistence: mockPersistence,
124127
appVersion: '1.0.0',
128+
watchdogClient: mockWatchdogClient,
125129
});
126130

127131
await logger.logDailyActiveIfNeeded();
128132

129-
assert(sendStub.calledOnce);
130-
const msg = sendStub.firstCall.args[0];
133+
assert(mockWatchdogClient.send.calledOnce);
134+
const msg = mockWatchdogClient.send.firstCall.args[0];
131135
assert.strictEqual(msg.type, IpcMessageType.EVENT);
132136
assert.strictEqual(
133137
msg.payload.daily_active?.days_since_last_active,

0 commit comments

Comments
 (0)