Skip to content

Commit

Permalink
refactor collectAsyncMetrics
Browse files Browse the repository at this point in the history
  • Loading branch information
edmundhung committed Aug 22, 2024
1 parent cf9827e commit 543f3da
Show file tree
Hide file tree
Showing 5 changed files with 356 additions and 256 deletions.
137 changes: 87 additions & 50 deletions packages/create-cloudflare/src/__tests__/metrics.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { off } from "process";

Check failure on line 1 in packages/create-cloudflare/src/__tests__/metrics.test.ts

View workflow job for this annotation

GitHub Actions / Checks

'off' is defined but never used. Allowed unused vars must match /^_/u

Check failure on line 1 in packages/create-cloudflare/src/__tests__/metrics.test.ts

View workflow job for this annotation

GitHub Actions / Checks

'off' is defined but never used
import { CancelError } from "@cloudflare/cli/error";
import { sendEvent } from "helpers/sparrow";
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import { collectCLIOutput, normalizeOutput } from "../../../cli/test-util";
import { version as c3Version } from "../../package.json";
import {
getDeviceId,
getSessionId,
getUserId,
readMetricsConfig,
writeMetricsConfig,
Expand All @@ -20,6 +23,11 @@ vi.mock("helpers/sparrow");
describe("createReporter", () => {
const deviceId = "test-device-id";
const userId = "test-user-id";
const sessionId = "session-id-for-test-only";
const os = {
platform: process.platform,
arch: process.arch,
};

beforeEach(() => {
vi.useFakeTimers();
Expand All @@ -31,6 +39,7 @@ describe("createReporter", () => {
});
vi.mocked(getDeviceId).mockReturnValue(deviceId);
vi.mocked(getUserId).mockReturnValue(userId);
vi.mocked(getSessionId).mockReturnValue(sessionId);
});

afterEach(() => {
Expand All @@ -43,10 +52,10 @@ describe("createReporter", () => {
const reporter = createReporter();
const process = reporter.collectAsyncMetrics({
eventPrefix: "c3 session",
props: {
sessionId: "example",
c3Version: "1.2.3",
os: { platform: "cf", arch: "test" },
startedProps: {
args: {
projectName: "app",
},
},
promise: () => promise,
});
Expand All @@ -57,9 +66,12 @@ describe("createReporter", () => {
userId,
timestamp: Date.now(),
properties: {
sessionId: "example",
c3Version: "1.2.3",
os: { platform: "cf", arch: "test" },
sessionId,
c3Version,
os,
args: {
projectName: "app",
},
},
});
expect(sendEvent).toBeCalledTimes(1);
Expand All @@ -76,9 +88,12 @@ describe("createReporter", () => {
userId,
timestamp: Date.now(),
properties: {
sessionId: "example",
c3Version: "1.2.3",
os: { platform: "cf", arch: "test" },
sessionId,
c3Version,
os,
args: {
projectName: "app",
},
durationMs: 1234,
},
});
Expand All @@ -90,10 +105,10 @@ describe("createReporter", () => {
const reporter = createReporter();
const process = reporter.collectAsyncMetrics({
eventPrefix: "c3 session",
props: {
sessionId: "example",
c3Version: "1.2.3",
os: { platform: "cf", arch: "test" },
startedProps: {
args: {
projectName: "app",
},
},
promise: () => promise,
});
Expand All @@ -104,9 +119,12 @@ describe("createReporter", () => {
userId,
timestamp: Date.now(),
properties: {
sessionId: "example",
c3Version: "1.2.3",
os: { platform: "cf", arch: "test" },
sessionId,
c3Version,
os,
args: {
projectName: "app",
},
},
});
expect(sendEvent).toBeCalledTimes(1);
Expand All @@ -122,9 +140,12 @@ describe("createReporter", () => {
userId,
timestamp: Date.now(),
properties: {
sessionId: "example",
c3Version: "1.2.3",
os: { platform: "cf", arch: "test" },
sessionId,
c3Version,
os,
args: {
projectName: "app",
},
durationMs: 1234,
},
});
Expand All @@ -136,10 +157,8 @@ describe("createReporter", () => {
const reporter = createReporter();
const process = reporter.collectAsyncMetrics({
eventPrefix: "c3 session",
props: {
sessionId: "example",
c3Version: "1.2.3",
os: { platform: "cf", arch: "test" },
startedProps: {
args: { projectName: "app" },
},
promise: () => promise,
});
Expand All @@ -150,9 +169,12 @@ describe("createReporter", () => {
userId,
timestamp: Date.now(),
properties: {
sessionId: "example",
c3Version: "1.2.3",
os: { platform: "cf", arch: "test" },
sessionId,
c3Version,
os,
args: {
projectName: "app",
},
},
});
expect(sendEvent).toBeCalledTimes(1);
Expand All @@ -168,9 +190,12 @@ describe("createReporter", () => {
userId,
timestamp: Date.now(),
properties: {
sessionId: "example",
c3Version: "1.2.3",
os: { platform: "cf", arch: "test" },
sessionId,
c3Version,
os,
args: {
projectName: "app",
},
durationMs: 1234,
error: {
message: "test error",
Expand All @@ -187,10 +212,10 @@ describe("createReporter", () => {

const run = reporter.collectAsyncMetrics({
eventPrefix: "c3 session",
props: {
sessionId: "example",
c3Version: "1.2.3",
os: { platform: "cf", arch: "test" },
startedProps: {
args: {
projectName: "app",
},
},
promise: () => promise,
});
Expand All @@ -201,9 +226,12 @@ describe("createReporter", () => {
userId,
timestamp: Date.now(),
properties: {
sessionId: "example",
c3Version: "1.2.3",
os: { platform: "cf", arch: "test" },
sessionId,
c3Version,
os,
args: {
projectName: "app",
},
},
});
expect(sendEvent).toBeCalledTimes(1);
Expand All @@ -219,9 +247,12 @@ describe("createReporter", () => {
userId,
timestamp: Date.now(),
properties: {
sessionId: "example",
c3Version: "1.2.3",
os: { platform: "cf", arch: "test" },
sessionId,
c3Version,
os,
args: {
projectName: "app",
},
durationMs: 1234,
},
});
Expand All @@ -233,10 +264,10 @@ describe("createReporter", () => {
const reporter = createReporter();
const run = reporter.collectAsyncMetrics({
eventPrefix: "c3 session",
props: {
sessionId: "example",
c3Version: "1.2.3",
os: { platform: "cf", arch: "test" },
startedProps: {
args: {
projectName: "app",
},
},
promise: () => promise,
});
Expand All @@ -247,9 +278,12 @@ describe("createReporter", () => {
userId,
timestamp: Date.now(),
properties: {
sessionId: "example",
c3Version: "1.2.3",
os: { platform: "cf", arch: "test" },
sessionId,
c3Version,
os,
args: {
projectName: "app",
},
},
});
expect(sendEvent).toBeCalledTimes(1);
Expand All @@ -265,9 +299,12 @@ describe("createReporter", () => {
userId,
timestamp: Date.now(),
properties: {
sessionId: "example",
c3Version: "1.2.3",
os: { platform: "cf", arch: "test" },
sessionId,
c3Version,
os,
args: {
projectName: "app",
},
durationMs: 1234,
},
});
Expand Down
2 changes: 1 addition & 1 deletion packages/create-cloudflare/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export const main = async (argv: string[]) => {
} else {
await reporter.collectAsyncMetrics({
eventPrefix: "c3 session",
props: {
startedProps: {
args,
},
promise: () => runCli(args),
Expand Down
Loading

0 comments on commit 543f3da

Please sign in to comment.