Files
gstack/browse/test/telemetry.test.ts
T
Garry Tan 35dd014c58 v1.87.5.0 perf: remove idle waits from tests and CI planning (#2897)
* v1.87.5.0 perf: remove idle waits from tests and CI planning

* fix: settle split PTY redraws before routing input

* docs: record final burst-safe test benchmarks

* fix: keep cold-setup snapshot metadata dependency-free

* fix: avoid early-reader pipe races in artifact URL parsing

* fix: preserve safety matches for multiline command payloads

* fix: recognize concurrent CSO publication removal

* test: preload the UI design-review target before invocation

* docs: record validation blocker fixes

* fix: bind plan observer rejection to the invoked command

* fix: count only native design decisions in the UI gate

* docs: clarify UI-positive eval evidence requirements

* test: recognize native UI decisions without weakening finding counts

* test: decouple native UI evidence from question punctuation

* test: recognize concrete native UI decisions independently of prose format

* fix: retain failed eval logs under the hidden CI cache

* test: await telemetry completion instead of racing disk writes
2026-09-21 12:27:25 -04:00

78 lines
3.1 KiB
TypeScript

import { describe, it, expect, beforeAll, beforeEach, afterAll } from 'bun:test';
import { promises as fs } from 'fs';
import * as path from 'path';
import * as os from 'os';
const TMP_HOME = path.join(os.tmpdir(), `gstack-telemetry-test-${process.pid}-${Date.now()}`);
const TELEMETRY_FILE = path.join(TMP_HOME, 'analytics', 'browse-telemetry.jsonl');
// Use GSTACK_HOME env to redirect telemetry writes (read each call,
// not cached at module-load).
// Scoped to this file's execution window — module-scope env assignment
// leaks into sibling files in the shard process (see
// test/gstack-home-module-scope.test.ts).
const ORIGINAL_GSTACK_HOME = process.env.GSTACK_HOME;
const ORIGINAL_TELEMETRY_OFF = process.env.GSTACK_TELEMETRY_OFF;
beforeAll(() => {
process.env.GSTACK_HOME = TMP_HOME;
process.env.GSTACK_TELEMETRY_OFF = '0';
});
afterAll(() => {
if (ORIGINAL_GSTACK_HOME === undefined) delete process.env.GSTACK_HOME;
else process.env.GSTACK_HOME = ORIGINAL_GSTACK_HOME;
if (ORIGINAL_TELEMETRY_OFF === undefined) delete process.env.GSTACK_TELEMETRY_OFF;
else process.env.GSTACK_TELEMETRY_OFF = ORIGINAL_TELEMETRY_OFF;
});
beforeEach(async () => {
await fs.rm(TMP_HOME, { recursive: true, force: true });
});
afterAll(async () => {
await fs.rm(TMP_HOME, { recursive: true, force: true });
});
async function readEvents(): Promise<any[]> {
try {
const raw = await fs.readFile(TELEMETRY_FILE, 'utf8');
return raw.trim().split('\n').filter(Boolean).map((l) => JSON.parse(l));
} catch {
return [];
}
}
describe('telemetry: signals fire to ~/.gstack/analytics/browse-telemetry.jsonl', () => {
it('logTelemetry writes a JSONL line with ts injected', async () => {
const { logTelemetry, _resetTelemetryCache } = await import('../src/telemetry');
_resetTelemetryCache();
await logTelemetry({ event: 'domain_skill_saved', host: 'test.com', scope: 'project', state: 'quarantined', bytes: 42 });
const events = await readEvents();
expect(events).toHaveLength(1);
expect(events[0].event).toBe('domain_skill_saved');
expect(events[0].host).toBe('test.com');
expect(events[0].bytes).toBe(42);
expect(events[0].ts).toMatch(/^\d{4}-\d{2}-\d{2}T/);
});
it('GSTACK_TELEMETRY_OFF=1 silences all events', async () => {
process.env.GSTACK_TELEMETRY_OFF = '1';
const { logTelemetry, _resetTelemetryCache } = await import('../src/telemetry');
_resetTelemetryCache();
await logTelemetry({ event: 'cdp_method_called', domain: 'X', method: 'y' });
const events = await readEvents();
expect(events).toHaveLength(0);
process.env.GSTACK_TELEMETRY_OFF = '0';
});
it('telemetry never throws even if disk fails', async () => {
// Point HOME to a path that doesn't exist + can't be created (root-owned)
// — but that's hard to set up cross-platform. Just check that calling
// logTelemetry on a missing directory doesn't throw.
const { logTelemetry, _resetTelemetryCache } = await import('../src/telemetry');
_resetTelemetryCache();
let completed: Promise<void> | undefined;
expect(() => { completed = logTelemetry({ event: 'noop_test' }); }).not.toThrow();
await completed;
});
});