|
| 1 | +import type { Payload } from "../../../../../src/internal/cli/telemetry/analytics/types.js"; |
| 2 | + |
| 3 | +import assert from "node:assert/strict"; |
| 4 | +import path from "node:path"; |
| 5 | +import { after, afterEach, before, beforeEach, describe, it } from "node:test"; |
| 6 | + |
| 7 | +import { getConfigDir } from "@ignored/hardhat-vnext-core/global-dir"; |
| 8 | +import { |
| 9 | + copy, |
| 10 | + exists, |
| 11 | + readJsonFile, |
| 12 | + remove, |
| 13 | + writeJsonFile, |
| 14 | +} from "@ignored/hardhat-vnext-utils/fs"; |
| 15 | + |
| 16 | +import { |
| 17 | + sendTaskAnalytics, |
| 18 | + sendTelemetryConsentAnalytics, |
| 19 | +} from "../../../../../src/internal/cli/telemetry/analytics/analytics.js"; |
| 20 | +import { getHardhatVersion } from "../../../../../src/internal/utils/package.js"; |
| 21 | + |
| 22 | +// The analytics logic uses a detached subprocess to send the payload via HTTP call. |
| 23 | +// We cannot test the HTTP call directly, but we can use a test subprocess to verify if the payload is correctly created. |
| 24 | +// This is possible because the analytics code attempts to execute a subprocess file of type 'JS'. JS files are only available after compilation. |
| 25 | +// During the tests, no JS file is available, so the expected subprocess does not exist. Therefore, we can copy a test subprocess file |
| 26 | +// to the expected location instead of the original one and check if it receives the correct payload. |
| 27 | + |
| 28 | +const PATH_TO_FIXTURE = path.join( |
| 29 | + process.cwd(), |
| 30 | + "test", |
| 31 | + "fixture-projects", |
| 32 | + "cli", |
| 33 | + "telemetry", |
| 34 | + "analytics", |
| 35 | +); |
| 36 | + |
| 37 | +const SOURCE_PATH_TEST_SUBPROCESS_FILE = path.join( |
| 38 | + PATH_TO_FIXTURE, |
| 39 | + "analytics-subprocess.js", |
| 40 | +); |
| 41 | + |
| 42 | +const DEST_PATH_TEST_SUBPROCESS_FILE = path.join( |
| 43 | + process.cwd(), |
| 44 | + "src", |
| 45 | + "internal", |
| 46 | + "cli", |
| 47 | + "telemetry", |
| 48 | + "analytics", |
| 49 | + "analytics-subprocess.js", |
| 50 | +); |
| 51 | + |
| 52 | +const RESULT_FILE_PATH = path.join(PATH_TO_FIXTURE, "result.json"); |
| 53 | + |
| 54 | +async function copyTestSubprocessFile() { |
| 55 | + await copy(SOURCE_PATH_TEST_SUBPROCESS_FILE, DEST_PATH_TEST_SUBPROCESS_FILE); |
| 56 | +} |
| 57 | + |
| 58 | +async function removeTestSubprocessFile() { |
| 59 | + remove(DEST_PATH_TEST_SUBPROCESS_FILE); |
| 60 | +} |
| 61 | + |
| 62 | +async function setTelemetryConsentFile(consent: boolean) { |
| 63 | + const configDir = await getConfigDir(); |
| 64 | + const filePath = path.join(configDir, "telemetry-consent.json"); |
| 65 | + await writeJsonFile(filePath, { consent }); |
| 66 | +} |
| 67 | + |
| 68 | +async function checkIfSubprocessWasExecuted() { |
| 69 | + // Checks if the subprocess was executed by waiting for a file to be created. |
| 70 | + // Uses an interval to periodically check for the file. If the file isn't found |
| 71 | + // within a specified number of attempts, an error is thrown, indicating a failure in subprocess execution. |
| 72 | + const MAX_COUNTER = 20; |
| 73 | + |
| 74 | + return new Promise((resolve, reject) => { |
| 75 | + let counter = 0; |
| 76 | + |
| 77 | + const intervalId = setInterval(async () => { |
| 78 | + counter++; |
| 79 | + |
| 80 | + if (await exists(RESULT_FILE_PATH)) { |
| 81 | + clearInterval(intervalId); |
| 82 | + resolve(true); |
| 83 | + } else if (counter > MAX_COUNTER) { |
| 84 | + clearInterval(intervalId); |
| 85 | + reject("Subprocess was not executed in the expected time"); |
| 86 | + } |
| 87 | + }, 100); |
| 88 | + }); |
| 89 | +} |
| 90 | + |
| 91 | +describe("analytics", () => { |
| 92 | + before(async () => { |
| 93 | + copyTestSubprocessFile(); |
| 94 | + }); |
| 95 | + |
| 96 | + after(async () => { |
| 97 | + await removeTestSubprocessFile(); |
| 98 | + }); |
| 99 | + |
| 100 | + beforeEach(async () => { |
| 101 | + await remove(RESULT_FILE_PATH); |
| 102 | + }); |
| 103 | + |
| 104 | + afterEach(async () => { |
| 105 | + await remove(RESULT_FILE_PATH); |
| 106 | + }); |
| 107 | + |
| 108 | + it("should create the correct payload for the telemetry consent", async () => { |
| 109 | + await sendTelemetryConsentAnalytics(true); |
| 110 | + |
| 111 | + await checkIfSubprocessWasExecuted(); |
| 112 | + |
| 113 | + const result = JSON.parse(await readJsonFile(RESULT_FILE_PATH)); |
| 114 | + |
| 115 | + const expected = await readJsonFile( |
| 116 | + path.join(PATH_TO_FIXTURE, "telemetry-consent-payload.json"), |
| 117 | + ); |
| 118 | + |
| 119 | + assert.deepEqual(result, expected); |
| 120 | + }); |
| 121 | + |
| 122 | + describe("analytics payload", async () => { |
| 123 | + const ORIGINAL_PROCESS_ENV = { ...process }; |
| 124 | + |
| 125 | + before(() => { |
| 126 | + // Force Ci to not be detected as Ci so the test can run (Ci is blocked for analytics) |
| 127 | + delete process.env.GITHUB_ACTIONS; |
| 128 | + delete process.env.NOW; |
| 129 | + delete process.env.DEPLOYMENT_ID; |
| 130 | + delete process.env.CODEBUILD_BUILD_NUMBER; |
| 131 | + delete process.env.CI; |
| 132 | + delete process.env.CONTINUOUS_INTEGRATION; |
| 133 | + delete process.env.BUILD_NUMBER; |
| 134 | + delete process.env.RUN_ID; |
| 135 | + |
| 136 | + process.stdout.isTTY = true; |
| 137 | + }); |
| 138 | + |
| 139 | + after(() => { |
| 140 | + process = ORIGINAL_PROCESS_ENV; |
| 141 | + }); |
| 142 | + |
| 143 | + it("should create the correct payload for the task analytics", async () => { |
| 144 | + await setTelemetryConsentFile(true); |
| 145 | + |
| 146 | + const wasSent = await sendTaskAnalytics("hardhat", "compile"); |
| 147 | + |
| 148 | + await checkIfSubprocessWasExecuted(); |
| 149 | + |
| 150 | + const result: Payload = JSON.parse(await readJsonFile(RESULT_FILE_PATH)); |
| 151 | + |
| 152 | + assert.equal(wasSent, true); |
| 153 | + |
| 154 | + // Check payload properties |
| 155 | + assert.notEqual(result.client_id, undefined); |
| 156 | + assert.notEqual(result.user_id, undefined); |
| 157 | + assert.equal(result.user_properties.projectId.value, "hardhat-project"); |
| 158 | + assert.equal(result.user_properties.userType.value, "Developer"); |
| 159 | + assert.equal( |
| 160 | + result.user_properties.hardhatVersion.value, |
| 161 | + await getHardhatVersion(), |
| 162 | + ); |
| 163 | + assert.notEqual(result.user_properties.operatingSystem.value, undefined); |
| 164 | + assert.notEqual(result.user_properties.nodeVersion.value, undefined); |
| 165 | + assert.equal(result.events[0].name, "task"); |
| 166 | + assert.equal(result.events[0].params.engagement_time_msec, "10000"); |
| 167 | + assert.notEqual(result.events[0].params.session_id, undefined); |
| 168 | + assert.equal(result.events[0].params.task, "hardhat"); |
| 169 | + assert.equal(result.events[0].params.scope, "compile"); |
| 170 | + }); |
| 171 | + }); |
| 172 | +}); |
0 commit comments