Skip to content

test: add unit tests for trace processing helpers - #2514

Open
ZayanKhan-12 wants to merge 1 commit into
ChromeDevTools:mainfrom
ZayanKhan-12:chore/tests-trace-guard
Open

test: add unit tests for trace processing helpers#2514
ZayanKhan-12 wants to merge 1 commit into
ChromeDevTools:mainfrom
ZayanKhan-12:chore/tests-trace-guard

Conversation

@ZayanKhan-12

Copy link
Copy Markdown
Contributor

Why

src/trace-processing/parse.ts exports several pure, browser-free helpers, but tests/trace-processing/parse.test.ts only exercised the happy parse path, the trace summary snapshot, and the undefined-buffer error. This PR adds direct unit tests for the remaining exported helpers and branches, so regressions in the discriminant logic or the error contracts are caught without needing a browser.

Coverage added

  • traceResultIsSuccess
    • returns true for the result of a real successful parse (fixture trace)
    • returns true for a TraceResult whose insights is null (the guard must only depend on parsedTrace)
    • returns false for the result of a failed parse
    • returns false for a TraceParseError object
    • returns false for a near-miss where the error message text is 'parsedTrace' — the guard checks the key, not values
  • parseRawTraceBuffer (previously untested error branches)
    • empty buffer → 'Decoding the trace buffer returned an empty string.'
    • invalid JSON → returns a TraceParseError instead of throwing
    • valid JSON that is not a trace → returns a TraceParseError instead of throwing
  • getInsightOutput (previously untested)
    • formatted output for a known insight (NAVIGATION_0 / LCPBreakdown from the web-dev-with-commit fixture)
    • error when the trace has no insights
    • error for an unknown insight set id
    • error for an unknown insight name

Tests only — no changes under src/, and no new snapshots (the insight output is asserted with a targeted match so devtools-frontend formatter updates don't churn a snapshot).

Testing

  • npm run build — clean
  • node scripts/test.js tests/trace-processing/parse.test.ts — 15/15 pass (3 suites)
  • npm run test:no-build — full suite green (exit 0)
  • npm run check-format — eslint + prettier clean

🤖 Generated with Claude Code

@google-cla

google-cla Bot commented Aug 8, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Adds direct unit tests for the pure helpers exported from
src/trace-processing/parse.ts:

- traceResultIsSuccess: accepts real parsed trace results (with and
  without insights) and rejects TraceParseError objects, including a
  near-miss where the error message contains the discriminant key name.
- parseRawTraceBuffer: covers the previously untested error branches for
  empty buffers, invalid JSON, and valid JSON that is not a trace.
- getInsightOutput: covers the formatted output for a known insight and
  all three error branches (no insights, unknown insight set id, unknown
  insight name).
@ZayanKhan-12
ZayanKhan-12 force-pushed the chore/tests-trace-guard branch from 7e939f4 to 14ccacf Compare August 8, 2026 21:46
@OrKoN
OrKoN requested a review from Lightning00Blade August 10, 2026 09:03
Comment on lines +128 to +130
if ('error' in insight) {
assert.fail(`Unexpected insight error: ${insight.error}`);
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if ('error' in insight) {
assert.fail(`Unexpected insight error: ${insight.error}`);
}
assert(!('error' in insight`),Unexpected insight error: ${insight.error}`);

Comment on lines +118 to +121
it('checks for the parsedTrace key, not the error message contents', () => {
const error: TraceParseError = {error: 'parsedTrace'};
assert.strictEqual(traceResultIsSuccess(error), false);
});

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
it('checks for the parsedTrace key, not the error message contents', () => {
const error: TraceParseError = {error: 'parsedTrace'};
assert.strictEqual(traceResultIsSuccess(error), false);
});

Comment on lines +85 to +88
if (traceResultIsSuccess(result)) {
assert.fail('Expected a parse error for a non-trace JSON input.');
}
assert.ok(result.error.length > 0);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (traceResultIsSuccess(result)) {
assert.fail('Expected a parse error for a non-trace JSON input.');
}
assert.ok(result.error.length > 0);
assert('error' in result);
assert.ok(result.error.length > 0);

Comment on lines +75 to +78
if (traceResultIsSuccess(result)) {
assert.fail('Expected a parse error for invalid JSON input.');
}
assert.match(result.error, /JSON/);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (traceResultIsSuccess(result)) {
assert.fail('Expected a parse error for invalid JSON input.');
}
assert.match(result.error, /JSON/);
assert('error' in result);
assert.match(result.error, /JSON/);

Comment on lines +27 to +29
if (!traceResultIsSuccess(result)) {
assert.fail(`Unexpected trace parse error: ${result.error}`);
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!traceResultIsSuccess(result)) {
assert.fail(`Unexpected trace parse error: ${result.error}`);
}
assert(traceResultIsSuccess(result), `Unexpected trace parse error: ${result.error}`);

});
});

describe('traceResultIsSuccess', () => {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this function we should just use a object that mimics the trace, so we can control it.
Also we can't use parseTrace as that itself include it.

Comment on lines 47 to 49
if ('error' in result) {
assert.fail(`Unexpected parse failure: ${result.error}`);
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert(!('error' in result), `Unexpected parse failure: ${result.error}`);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants