|
| 1 | +import { APM_STATS_WINDOW, type SpanLineStat } from "@posthog/shared"; |
| 2 | +import { describe, expect, it } from "vitest"; |
| 3 | +import { formatApmInlineComments } from "./apm-comment-formatter.js"; |
| 4 | + |
| 5 | +function stat(overrides: Partial<SpanLineStat>): SpanLineStat { |
| 6 | + return { |
| 7 | + line: 1, |
| 8 | + count: 100, |
| 9 | + errorCount: 0, |
| 10 | + p50Ms: 1, |
| 11 | + p95Ms: 2, |
| 12 | + ...overrides, |
| 13 | + }; |
| 14 | +} |
| 15 | + |
| 16 | +describe("formatApmInlineComments", () => { |
| 17 | + const src = ["fn a() {}", "fn b() {}", "fn c() {}"].join("\n"); |
| 18 | + const FILE = "rust/feature-flags/src/flags/flag_matching.rs"; |
| 19 | + |
| 20 | + it("appends an APM suffix to the line the stat points at (1-based)", () => { |
| 21 | + const out = formatApmInlineComments( |
| 22 | + src, |
| 23 | + "rust", |
| 24 | + [stat({ line: 2, p95Ms: 4.8 })], |
| 25 | + FILE, |
| 26 | + ); |
| 27 | + const lines = out.split("\n"); |
| 28 | + expect(lines[0]).toBe("fn a() {}"); |
| 29 | + expect(lines[1]).toContain("fn b() {}"); |
| 30 | + expect(lines[1]).toContain("[PostHog] APM"); |
| 31 | + expect(lines[1]).toContain("4.8"); |
| 32 | + expect(lines[2]).toBe("fn c() {}"); |
| 33 | + }); |
| 34 | + |
| 35 | + it("includes a self-contained, line-specific query-apm-spans drill-in hint", () => { |
| 36 | + const out = formatApmInlineComments(src, "rust", [stat({ line: 2 })], FILE); |
| 37 | + const line = out.split("\n")[1]; |
| 38 | + expect(line).toContain("query-apm-spans"); |
| 39 | + expect(line).toContain('code.filepath~"flag_matching.rs"'); |
| 40 | + expect(line).toContain("code.lineno=2"); |
| 41 | + }); |
| 42 | + |
| 43 | + it("uses # comments for python/ruby", () => { |
| 44 | + const out = formatApmInlineComments( |
| 45 | + "def a():\n pass", |
| 46 | + "python", |
| 47 | + [stat({ line: 1 })], |
| 48 | + "svc/main.py", |
| 49 | + ); |
| 50 | + expect(out.split("\n")[0]).toMatch(/# \[PostHog\] APM/); |
| 51 | + }); |
| 52 | + |
| 53 | + it("surfaces error count only when there are errors", () => { |
| 54 | + const withErr = formatApmInlineComments( |
| 55 | + src, |
| 56 | + "rust", |
| 57 | + [stat({ line: 1, errorCount: 3, count: 100 })], |
| 58 | + FILE, |
| 59 | + ); |
| 60 | + expect(withErr.split("\n")[0]).toContain("3 errors"); |
| 61 | + |
| 62 | + const noErr = formatApmInlineComments( |
| 63 | + src, |
| 64 | + "rust", |
| 65 | + [stat({ line: 1, errorCount: 0 })], |
| 66 | + FILE, |
| 67 | + ); |
| 68 | + // "errors" must not appear; the hint uses no such word, so this is safe. |
| 69 | + expect(noErr.split("\n")[0]).not.toContain("errors"); |
| 70 | + }); |
| 71 | + |
| 72 | + it("ignores stats whose line is out of range", () => { |
| 73 | + expect( |
| 74 | + formatApmInlineComments(src, "rust", [stat({ line: 99 })], FILE), |
| 75 | + ).toBe(src); |
| 76 | + }); |
| 77 | + |
| 78 | + it("promotes a count that rounds up to the next unit (no '1000.0k')", () => { |
| 79 | + const out = formatApmInlineComments( |
| 80 | + src, |
| 81 | + "rust", |
| 82 | + [stat({ line: 1, count: 999_999 })], |
| 83 | + FILE, |
| 84 | + ); |
| 85 | + const line = out.split("\n")[0]; |
| 86 | + expect(line).toContain("1.0M"); |
| 87 | + expect(line).not.toContain("1000.0k"); |
| 88 | + }); |
| 89 | + |
| 90 | + it("includes p99 and the window-labelled span count", () => { |
| 91 | + const out = formatApmInlineComments( |
| 92 | + src, |
| 93 | + "rust", |
| 94 | + [stat({ line: 1, count: 26_200, p99Ms: 12 })], |
| 95 | + FILE, |
| 96 | + ); |
| 97 | + const line = out.split("\n")[0]; |
| 98 | + expect(line).toContain("p99 12ms"); |
| 99 | + expect(line).toContain(`spans/${APM_STATS_WINDOW.short}`); |
| 100 | + }); |
| 101 | + |
| 102 | + it("appends period-over-period deltas to the metrics that changed", () => { |
| 103 | + const out = formatApmInlineComments( |
| 104 | + src, |
| 105 | + "rust", |
| 106 | + [ |
| 107 | + stat({ |
| 108 | + line: 1, |
| 109 | + count: 1000, |
| 110 | + p50Ms: 1.5, |
| 111 | + p95Ms: 7, |
| 112 | + p99Ms: 13, |
| 113 | + p50PctChange: 12, |
| 114 | + p95PctChange: 180, |
| 115 | + p99PctChange: null, |
| 116 | + countPctChange: 40, |
| 117 | + }), |
| 118 | + ], |
| 119 | + FILE, |
| 120 | + ); |
| 121 | + const line = out.split("\n")[0]; |
| 122 | + expect(line).toContain("p50 1.5ms (+12%)"); |
| 123 | + expect(line).toContain("p95 7ms (+180%)"); |
| 124 | + expect(line).toContain(`spans/${APM_STATS_WINDOW.short} (+40%)`); |
| 125 | + // p99 had no baseline (null) → no delta token on it. |
| 126 | + expect(line).not.toContain("p99 13ms ("); |
| 127 | + }); |
| 128 | + |
| 129 | + it("keeps CRLF line endings intact (comment before the carriage return)", () => { |
| 130 | + const crlf = ["fn a() {}", "fn b() {}"].join("\r\n"); |
| 131 | + const out = formatApmInlineComments( |
| 132 | + crlf, |
| 133 | + "rust", |
| 134 | + [stat({ line: 1 })], |
| 135 | + FILE, |
| 136 | + ); |
| 137 | + const outLines = out.split("\r\n"); |
| 138 | + expect(outLines).toHaveLength(2); |
| 139 | + expect(outLines[0]).toContain("[PostHog] APM"); |
| 140 | + expect(outLines[0]).not.toContain("\r"); |
| 141 | + expect(outLines[1]).toBe("fn b() {}"); |
| 142 | + }); |
| 143 | +}); |
0 commit comments