项目文件夹

文件
Ergün Erdoğmuş a3de5e4227 chore: implement telemetry persistence and daily active logging (#769)
This PR implements the persistence layer for the telemetry system. It
introduces `FilePersistence` for local state management and integrates
it with `ClearcutLogger` to support "Daily Active" metric. I have
decided not to send `first_time_installation` events since we can deduce
them from `daily active` events where the `days_since_last_active` will
be `-1` for that case.

**Implementation Roadmap:**
This is the third in a series of PRs designed to implement the telemetry
system:
1. **CLI & Opt-out Mechanism
([Merged](https://github.com/ChromeDevTools/chrome-devtools-mcp/pull/757)):**
    *   Added `--usage-statistics` flag and transparency logging.
2. **Logger Scaffolding & Integration
([Merged](https://github.com/ChromeDevTools/chrome-devtools-mcp/pull/758)):**
    *   **`ClearcutLogger`**: Implemented the main logging entry point.
* **One-way Data Flow**: Integrated `logToolInvocation` and
`logServerStart` hooks into `main.ts` to capture events.
    *   **`ClearcutSender`**: Introduced a transport abstraction.
* **Type Definitions**: Added TypeScript definitions for the telemetry
Protocol Buffer messages.
3.  **Persistence Layer (This PR):**
* **`FilePersistence`**: Implemented a local file-based state manager to
persist the `lastActive` timestamp.
* **Daily Active Logic**: Integrated persistence into `ClearcutLogger`
to automatically detect and log `daily_active` events (with
`days_since_last_active` calculation) via `logDailyActiveIfNeeded`.
4.  **Watchdog Process Architecture (Next):**
* Move `ClearcutSender` execution to a dedicated watchdog process to
ensure reliable event transmission even during abrupt server shutdowns.
5.  **Transport, Batching & Retries (Next):**
* Finalize `ClearcutSender` with actual HTTP transport logic, including
event batching and exponential backoff retries.
2026-01-15 10:57:40 +00:00

71 行
1.9 KiB
TypeScript

/**
* @license
* Copyright 2026 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import assert from 'node:assert';
import crypto from 'node:crypto';
import fs from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import {describe, it, afterEach, beforeEach} from 'node:test';
import * as persistence from '../../src/telemetry/persistence.js';
describe('FilePersistence', () => {
let tmpDir: string;
beforeEach(async () => {
tmpDir = path.join(
await fs.realpath(os.tmpdir()),
`telemetry-test-${crypto.randomUUID()}`,
);
await fs.mkdir(tmpDir, {recursive: true});
});
afterEach(async () => {
await fs.rm(tmpDir, {recursive: true, force: true});
});
describe('loadState', () => {
it('returns default state if file does not exist', async () => {
const filePersistence = new persistence.FilePersistence(tmpDir);
const state = await filePersistence.loadState();
assert.deepStrictEqual(state, {
lastActive: '',
});
});
it('returns stored state if file exists', async () => {
const expectedState = {
lastActive: '2023-01-01T00:00:00.000Z',
};
await fs.writeFile(
path.join(tmpDir, 'telemetry_state.json'),
JSON.stringify(expectedState),
);
const filePersistence = new persistence.FilePersistence(tmpDir);
const state = await filePersistence.loadState();
assert.deepStrictEqual(state, expectedState);
});
});
describe('saveState', () => {
it('saves state to file', async () => {
const state = {
lastActive: '2023-01-01T00:00:00.000Z',
};
const filePersistence = new persistence.FilePersistence(tmpDir);
await filePersistence.saveState(state);
const content = await fs.readFile(
path.join(tmpDir, 'telemetry_state.json'),
'utf-8',
);
assert.deepStrictEqual(JSON.parse(content), state);
});
});
});