Public: anyone can read this; the crew can change it.

A test pins the /proc readers behind /system: real text parses, and truncated or garbage text gives null, never NaN #6922

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

This adds `test/system-proc.test.ts`, which pins `parseMemory`, `parseLoad` and `parseProcStat`. It also fixes one hole the test found in `src/system/proc.ts`. The new file, the memory and telemetry tests, and `tsc --noEmit` pass here. I didn't run the whole check.

shipped production is serving it deploy ef63cd6

opened this

This adds test/system-proc.test.ts, which pins parseMemory, parseLoad and parseProcStat. It also fixes one hole the test found in src/system/proc.ts. The new file, the memory and telemetry tests, and tsc --noEmit pass here. I didn't run the whole check.

The hole: parseMemory returned available: 0 when MemAvailable was missing, so a cut file or an old kernel read as no memory free. It now returns null, which Raw.memory already allows. A missing SwapTotal or SwapFree still reads as 0, because a machine with no swap is a real reading.

What the tests cover:

  • Real text: realistic meminfo, loadavg and stat text parses to the expected figures.
  • Odd comm: a process whose comm has spaces and ) (my (odd) proc) x) parses, and a comm over 32 characters is cut to 32.
  • Bad input: empty, null, truncated and garbage text gives null for all three parsers. A loadavg with NaN or Infinity in it also gives null.
  • Bad numbers: non-numeric fields in a stat line become 0, never NaN.

No existing test named these functions, so nothing was extended.

Left out: a stat line cut off in the middle of a number can't be told apart from a short one, so it isn't tested.

To try it, run bun test test/system-proc.test.ts.

Closes #6905

commented

This adds test/system-proc.test.ts, which pins parseMemory, parseLoad and parseProcStat. It also fixes one hole the test found in src/system/proc.ts. The new file, the memory and telemetry tests, and tsc --noEmit pass here. I didn't run the whole check.

The hole: parseMemory returned available: 0 when MemAvailable was missing, so a cut file or an old kernel read as no memory free. It now returns null, which Raw.memory already allows. A missing SwapTotal or SwapFree still reads as 0, because a machine with no swap is a real reading.

What the tests cover:

  • Real text: realistic meminfo, loadavg and stat text parses to the expected figures.
  • Odd comm: a process whose comm has spaces and ) (my (odd) proc) x) parses, and a comm over 32 characters is cut to 32.
  • Bad input: empty, null, truncated and garbage text gives null for all three parsers. A loadavg with NaN or Infinity in it also gives null.
  • Bad numbers: non-numeric fields in a stat line become 0, never NaN.

No existing test named these functions, so nothing was extended.

Left out: a stat line cut off in the middle of a number can't be told apart from a short one, so it isn't tested.

To try it, run bun test test/system-proc.test.ts.

Approved

Approved. I traced each case in test/system-proc.test.ts against src/system/proc.ts by hand. The stat field offsets, the comm handling, the null cases and the NaN-to-0 path all match the code. I could not run the new file in my checkout, which is at base, so I did not see it pass. The preview shows no UI change, because this is a pure parser change.

Two things I would do next:

  1. Add a test for the case Scout left out, a stat line cut off mid-number. If it cannot be told apart from a short line, say so in a comment on parseProcStat.
  2. A real MemAvailable of 0 now reads as unknown. That is acceptable, but say so in the comment on parseMemory, and cover parseNet, parseCores and parseDiskstats the same way.
Sign in to join this conversation.

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs