2024-02-02 06:15:29 -05:00
|
|
|
import assert from 'node:assert/strict';
|
2024-02-21 09:08:19 -05:00
|
|
|
import { after, before, describe, it } from 'node:test';
|
2022-06-27 16:17:40 -05:00
|
|
|
import { AstroTelemetry } from '../dist/index.js';
|
2022-06-27 16:16:07 -05:00
|
|
|
|
2023-08-28 16:03:01 -05:00
|
|
|
function setup() {
|
|
|
|
const config = new Map();
|
|
|
|
const telemetry = new AstroTelemetry({ version: '0.0.0-test.1' });
|
|
|
|
const logs = [];
|
|
|
|
// Stub isCI to false so we can test user-facing behavior
|
|
|
|
telemetry.isCI = false;
|
|
|
|
// Stub process.env to properly test in Astro's own CI
|
|
|
|
telemetry.env = {};
|
|
|
|
// Override config so we can inspect it
|
|
|
|
telemetry.config = config;
|
|
|
|
// Override debug so we can inspect it
|
|
|
|
telemetry.debug.enabled = true;
|
|
|
|
telemetry.debug.log = (...args) => logs.push(args);
|
|
|
|
|
2023-08-28 16:05:20 -05:00
|
|
|
return { telemetry, config, logs };
|
2023-08-28 16:03:01 -05:00
|
|
|
}
|
2023-08-28 16:05:20 -05:00
|
|
|
describe('AstroTelemetry', () => {
|
2023-08-28 16:03:01 -05:00
|
|
|
let oldCI;
|
|
|
|
before(() => {
|
|
|
|
oldCI = process.env.CI;
|
|
|
|
// Stub process.env.CI to `false`
|
|
|
|
process.env.CI = 'false';
|
2023-08-28 16:05:20 -05:00
|
|
|
});
|
2023-08-28 16:03:01 -05:00
|
|
|
after(() => {
|
|
|
|
process.env.CI = oldCI;
|
2023-08-28 16:05:20 -05:00
|
|
|
});
|
2022-06-27 16:16:07 -05:00
|
|
|
it('initializes when expected arguments are given', () => {
|
2023-08-28 16:03:01 -05:00
|
|
|
const { telemetry } = setup();
|
2024-02-02 06:15:29 -05:00
|
|
|
assert(telemetry instanceof AstroTelemetry);
|
2022-06-27 16:16:07 -05:00
|
|
|
});
|
2023-08-28 16:03:01 -05:00
|
|
|
it('does not record event if disabled', async () => {
|
|
|
|
const { telemetry, config, logs } = setup();
|
|
|
|
telemetry.setEnabled(false);
|
|
|
|
const [key] = Array.from(config.keys());
|
2024-02-02 06:15:29 -05:00
|
|
|
assert.notEqual(key, undefined);
|
|
|
|
assert.equal(config.get(key), false);
|
|
|
|
assert.equal(telemetry.enabled, false);
|
|
|
|
assert.equal(telemetry.isDisabled, true);
|
2023-08-28 16:03:01 -05:00
|
|
|
const result = await telemetry.record(['TEST']);
|
2024-02-02 06:15:29 -05:00
|
|
|
assert.equal(result, undefined);
|
2023-08-28 16:03:01 -05:00
|
|
|
const [log] = logs;
|
2024-02-02 06:15:29 -05:00
|
|
|
assert.notEqual(log, undefined);
|
|
|
|
assert.match(logs.join(''), /disabled/);
|
2023-08-28 16:03:01 -05:00
|
|
|
});
|
|
|
|
it('records event if enabled', async () => {
|
|
|
|
const { telemetry, config, logs } = setup();
|
|
|
|
telemetry.setEnabled(true);
|
|
|
|
const [key] = Array.from(config.keys());
|
2024-02-02 06:15:29 -05:00
|
|
|
assert.notEqual(key, undefined);
|
|
|
|
assert.equal(config.get(key), true);
|
|
|
|
assert.equal(telemetry.enabled, true);
|
|
|
|
assert.equal(telemetry.isDisabled, false);
|
2023-08-28 16:03:01 -05:00
|
|
|
await telemetry.record(['TEST']);
|
2024-02-02 06:15:29 -05:00
|
|
|
assert.equal(logs.length, 2);
|
2023-08-28 16:03:01 -05:00
|
|
|
});
|
|
|
|
it('respects disable from notify', async () => {
|
|
|
|
const { telemetry, config, logs } = setup();
|
|
|
|
await telemetry.notify(() => false);
|
|
|
|
const [key] = Array.from(config.keys());
|
2024-02-02 06:15:29 -05:00
|
|
|
assert.notEqual(key, undefined);
|
|
|
|
assert.equal(config.get(key), false);
|
|
|
|
assert.equal(telemetry.enabled, false);
|
|
|
|
assert.equal(telemetry.isDisabled, true);
|
2023-08-28 16:03:01 -05:00
|
|
|
const [log] = logs;
|
2024-02-02 06:15:29 -05:00
|
|
|
assert.notEqual(log, undefined);
|
|
|
|
assert.match(logs.join(''), /disabled/);
|
2023-08-28 16:03:01 -05:00
|
|
|
});
|
|
|
|
it('respects enable from notify', async () => {
|
|
|
|
const { telemetry, config, logs } = setup();
|
|
|
|
await telemetry.notify(() => true);
|
|
|
|
const [key] = Array.from(config.keys());
|
2024-02-02 06:15:29 -05:00
|
|
|
assert.notEqual(key, undefined);
|
|
|
|
assert.equal(config.get(key), true);
|
|
|
|
assert.equal(telemetry.enabled, true);
|
|
|
|
assert.equal(telemetry.isDisabled, false);
|
2023-08-28 16:03:01 -05:00
|
|
|
const [log] = logs;
|
2024-02-02 06:15:29 -05:00
|
|
|
assert.notEqual(log, undefined);
|
|
|
|
assert.match(logs.join(''), /enabled/);
|
2023-08-28 16:03:01 -05:00
|
|
|
});
|
2022-06-27 16:16:07 -05:00
|
|
|
});
|