2022-06-27 21:16:07 +00:00
|
|
|
import { expect } from 'chai';
|
2022-06-27 21:17:40 +00:00
|
|
|
import { AstroTelemetry } from '../dist/index.js';
|
2022-06-27 21:16:07 +00:00
|
|
|
|
2023-08-28 21:03:01 +00: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 21:05:20 +00:00
|
|
|
return { telemetry, config, logs };
|
2023-08-28 21:03:01 +00:00
|
|
|
}
|
2023-08-28 21:05:20 +00:00
|
|
|
describe('AstroTelemetry', () => {
|
2023-08-28 21:03:01 +00:00
|
|
|
let oldCI;
|
|
|
|
before(() => {
|
|
|
|
oldCI = process.env.CI;
|
|
|
|
// Stub process.env.CI to `false`
|
|
|
|
process.env.CI = 'false';
|
2023-08-28 21:05:20 +00:00
|
|
|
});
|
2023-08-28 21:03:01 +00:00
|
|
|
after(() => {
|
|
|
|
process.env.CI = oldCI;
|
2023-08-28 21:05:20 +00:00
|
|
|
});
|
2022-06-27 21:16:07 +00:00
|
|
|
it('initializes when expected arguments are given', () => {
|
2023-08-28 21:03:01 +00:00
|
|
|
const { telemetry } = setup();
|
2022-06-27 21:17:40 +00:00
|
|
|
expect(telemetry).to.be.instanceOf(AstroTelemetry);
|
2022-06-27 21:16:07 +00:00
|
|
|
});
|
2023-08-28 21:03:01 +00:00
|
|
|
it('does not record event if disabled', async () => {
|
|
|
|
const { telemetry, config, logs } = setup();
|
|
|
|
telemetry.setEnabled(false);
|
|
|
|
const [key] = Array.from(config.keys());
|
|
|
|
expect(key).not.to.be.undefined;
|
|
|
|
expect(config.get(key)).to.be.false;
|
|
|
|
expect(telemetry.enabled).to.be.false;
|
|
|
|
expect(telemetry.isDisabled).to.be.true;
|
|
|
|
const result = await telemetry.record(['TEST']);
|
|
|
|
expect(result).to.be.undefined;
|
|
|
|
const [log] = logs;
|
|
|
|
expect(log).not.to.be.undefined;
|
|
|
|
expect(logs.join('')).to.match(/disabled/);
|
|
|
|
});
|
|
|
|
it('records event if enabled', async () => {
|
|
|
|
const { telemetry, config, logs } = setup();
|
|
|
|
telemetry.setEnabled(true);
|
|
|
|
const [key] = Array.from(config.keys());
|
|
|
|
expect(key).not.to.be.undefined;
|
|
|
|
expect(config.get(key)).to.be.true;
|
|
|
|
expect(telemetry.enabled).to.be.true;
|
|
|
|
expect(telemetry.isDisabled).to.be.false;
|
|
|
|
await telemetry.record(['TEST']);
|
|
|
|
expect(logs.length).to.equal(2);
|
|
|
|
});
|
|
|
|
it('respects disable from notify', async () => {
|
|
|
|
const { telemetry, config, logs } = setup();
|
|
|
|
await telemetry.notify(() => false);
|
|
|
|
const [key] = Array.from(config.keys());
|
|
|
|
expect(key).not.to.be.undefined;
|
|
|
|
expect(config.get(key)).to.be.false;
|
|
|
|
expect(telemetry.enabled).to.be.false;
|
|
|
|
expect(telemetry.isDisabled).to.be.true;
|
|
|
|
const [log] = logs;
|
|
|
|
expect(log).not.to.be.undefined;
|
|
|
|
expect(logs.join('')).to.match(/disabled/);
|
|
|
|
});
|
|
|
|
it('respects enable from notify', async () => {
|
|
|
|
const { telemetry, config, logs } = setup();
|
|
|
|
await telemetry.notify(() => true);
|
|
|
|
const [key] = Array.from(config.keys());
|
|
|
|
expect(key).not.to.be.undefined;
|
|
|
|
expect(config.get(key)).to.be.true;
|
|
|
|
expect(telemetry.enabled).to.be.true;
|
|
|
|
expect(telemetry.isDisabled).to.be.false;
|
|
|
|
const [log] = logs;
|
|
|
|
expect(log).not.to.be.undefined;
|
|
|
|
expect(logs.join('')).to.match(/enabled/);
|
|
|
|
});
|
2022-06-27 21:16:07 +00:00
|
|
|
});
|