Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions apps/control-panel/src/lib/experiments/metric-names.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { describe, expect, it } from "vitest";
import { metricDisplayName, withMetricNames } from "./metric-names";

describe("withMetricNames", () => {
it("substitutes an ordinary Metric display name", () => {
expect(withMetricNames("metric_x failed", new Map([["metric_x", "Checkout conversion"]]))).toBe(
"Checkout conversion failed",
);
});

it("treats $& as literal authored text, not the matched Metric id", () => {
expect(withMetricNames("metric_x failed", new Map([["metric_x", "Revenue $&"]]))).toBe(
"Revenue $& failed",
);
});

it("treats $' as literal authored text, not the unmatched suffix", () => {
expect(withMetricNames("metric_x failed", new Map([["metric_x", "Revenue $'"]]))).toBe(
"Revenue $' failed",
);
});

it("treats $ as literal authored text", () => {
expect(withMetricNames("metric_x failed", new Map([["metric_x", "Revenue $"]]))).toBe(
"Revenue $ failed",
);
});

it("leaves a deleted Metric id in place when no display name remains", () => {
expect(withMetricNames("metric_x failed", new Map())).toBe("metric_x failed");
});
});

describe("metricDisplayName", () => {
it("falls back to the raw Metric id after the catalog entry is gone", () => {
expect(metricDisplayName("metric_x", new Map())).toBe("metric_x");
});

it("returns the authored display name when the catalog still has it", () => {
expect(metricDisplayName("metric_x", new Map([["metric_x", "Revenue $&"]]))).toBe("Revenue $&");
});
});
2 changes: 1 addition & 1 deletion apps/control-panel/src/lib/experiments/metric-names.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ export function metricDisplayName(metricId: string, names: MetricNames): string
/** Gate check details arrive as prose with raw Metric ids embedded; rewrite them. */
export function withMetricNames(text: string, names: MetricNames): string {
let renamed = text;
for (const [id, name] of names) renamed = renamed.replaceAll(id, name);
for (const [id, name] of names) renamed = renamed.replaceAll(id, () => name);
return renamed;
}
Loading