-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathClearcutLogger.test.ts
More file actions
230 lines (193 loc) · 6.92 KB
/
ClearcutLogger.test.ts
File metadata and controls
230 lines (193 loc) · 6.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/**
* @license
* Copyright 2026 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import assert from 'node:assert';
import {describe, it, afterEach, beforeEach} from 'node:test';
import sinon from 'sinon';
import {DAEMON_CLIENT_NAME} from '../../src/daemon/utils.js';
import {
ClearcutLogger,
sanitizeParams,
} from '../../src/telemetry/ClearcutLogger.js';
import type {Persistence} from '../../src/telemetry/persistence.js';
import {FilePersistence} from '../../src/telemetry/persistence.js';
import {WatchdogMessageType} from '../../src/telemetry/types.js';
import {WatchdogClient} from '../../src/telemetry/WatchdogClient.js';
import {zod} from '../../src/third_party/index.js';
describe('ClearcutLogger', () => {
let mockPersistence: sinon.SinonStubbedInstance<Persistence>;
let mockWatchdogClient: sinon.SinonStubbedInstance<WatchdogClient>;
beforeEach(() => {
mockPersistence = sinon.createStubInstance(FilePersistence, {
loadState: Promise.resolve({
lastActive: '',
}),
});
mockWatchdogClient = sinon.createStubInstance(WatchdogClient);
});
afterEach(() => {
sinon.restore();
});
describe('logToolInvocation', () => {
it('sends correct payload', async () => {
const logger = new ClearcutLogger({
persistence: mockPersistence,
appVersion: '1.0.0',
watchdogClient: mockWatchdogClient,
});
await logger.logToolInvocation({
toolName: 'test_tool',
success: true,
latencyMs: 123,
});
assert(mockWatchdogClient.send.calledOnce);
const msg = mockWatchdogClient.send.firstCall.args[0];
assert.strictEqual(msg.type, WatchdogMessageType.LOG_EVENT);
assert.strictEqual(msg.payload.tool_invocation?.tool_name, 'test_tool');
assert.strictEqual(msg.payload.tool_invocation?.success, true);
assert.strictEqual(msg.payload.tool_invocation?.latency_ms, 123);
});
});
describe('setClientName', () => {
const clients = [
{name: 'claude-code', expected: 1}, // MCP_CLIENT_CLAUDE_CODE
{name: 'gemini-cli', expected: 2}, // MCP_CLIENT_GEMINI_CLI
{name: DAEMON_CLIENT_NAME, expected: 4}, // MCP_CLIENT_DT_MCP_CLI
{name: 'openclaw-browser', expected: 5}, // MCP_CLIENT_OPENCLAW
{name: 'codex-mcp-client', expected: 6}, // MCP_CLIENT_CODEX
{name: 'antigravity-client', expected: 7}, // MCP_CLIENT_ANTIGRAVITY
];
for (const {name, expected} of clients) {
it(`maps ${name} client correctly`, async () => {
const logger = new ClearcutLogger({
persistence: mockPersistence,
appVersion: '1.0.0',
watchdogClient: mockWatchdogClient,
});
logger.setClientName(name);
await logger.logServerStart({headless: true});
assert(mockWatchdogClient.send.calledOnce);
const msg = mockWatchdogClient.send.firstCall.args[0];
assert.strictEqual(msg.type, WatchdogMessageType.LOG_EVENT);
assert.strictEqual(msg.payload.mcp_client, expected);
});
}
});
describe('logServerStart', () => {
it('logs flag usage', async () => {
const logger = new ClearcutLogger({
persistence: mockPersistence,
appVersion: '1.0.0',
watchdogClient: mockWatchdogClient,
});
await logger.logServerStart({headless: true});
assert(mockWatchdogClient.send.calledOnce);
const msg = mockWatchdogClient.send.firstCall.args[0];
assert.strictEqual(msg.type, WatchdogMessageType.LOG_EVENT);
assert.strictEqual(msg.payload.server_start?.flag_usage?.headless, true);
});
});
describe('logDailyActiveIfNeeded', () => {
it('logs daily active if needed (lastActive > 24h ago)', async () => {
const yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
mockPersistence.loadState.resolves({
lastActive: yesterday.toISOString(),
});
const logger = new ClearcutLogger({
persistence: mockPersistence,
appVersion: '1.0.0',
watchdogClient: mockWatchdogClient,
});
await logger.logDailyActiveIfNeeded();
assert(mockWatchdogClient.send.calledOnce);
const msg = mockWatchdogClient.send.firstCall.args[0];
assert.strictEqual(msg.type, WatchdogMessageType.LOG_EVENT);
assert.ok(msg.payload.daily_active);
assert(mockPersistence.saveState.called);
});
it('does not log daily active if not needed (today)', async () => {
mockPersistence.loadState.resolves({
lastActive: new Date().toISOString(),
});
const logger = new ClearcutLogger({
persistence: mockPersistence,
appVersion: '1.0.0',
watchdogClient: mockWatchdogClient,
});
await logger.logDailyActiveIfNeeded();
assert(mockWatchdogClient.send.notCalled);
assert(mockPersistence.saveState.notCalled);
});
it('logs daily active with -1 if lastActive is missing', async () => {
mockPersistence.loadState.resolves({
lastActive: '',
});
const logger = new ClearcutLogger({
persistence: mockPersistence,
appVersion: '1.0.0',
watchdogClient: mockWatchdogClient,
});
await logger.logDailyActiveIfNeeded();
assert(mockWatchdogClient.send.calledOnce);
const msg = mockWatchdogClient.send.firstCall.args[0];
assert.strictEqual(msg.type, WatchdogMessageType.LOG_EVENT);
assert.strictEqual(msg.payload.daily_active?.days_since_last_active, -1);
assert(mockPersistence.saveState.called);
});
});
describe('sanitizeParams', () => {
it('filters out uid and transforms strings and arrays', () => {
const schema = {
uid: zod.string(),
myString: zod.string(),
myArray: zod.array(zod.string()),
myNumber: zod.number(),
myBool: zod.boolean(),
myEnum: zod.enum(['a', 'b']),
};
const params = {
uid: 'sensitive',
myString: 'hello',
myArray: ['one', 'two'],
myNumber: 42,
myBool: true,
myEnum: 'a' as const,
};
const sanitized = sanitizeParams(params, schema);
assert.deepStrictEqual(sanitized, {
my_string_length: 5,
my_array_count: 2,
my_number: 42,
my_bool: true,
my_enum: 'a',
});
});
it('throws error for unsupported types', () => {
const schema = {
myObj: zod.object({foo: zod.string()}),
};
const params = {
myObj: {foo: 'bar'},
};
assert.throws(
() => sanitizeParams(params, schema),
/Unsupported zod type for tool parameter: ZodObject/,
);
});
it('throws error when value is not of equivalent type', () => {
const schema = {
myString: zod.string(),
};
const params = {
myString: 123,
};
assert.throws(
() => sanitizeParams(params, schema),
/parameter myString has type ZodString but value 123 is not of equivalent type/,
);
});
});
});