/** * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * * @format * @oncall memory_lab */ /** * Unit tests for the compact report default: `memlab_round_digest` and * `memlab_leak_report` print their compact form on a repeat call for the same * run, `compact: false` restores the full report, and an explicit `compact` * leaves the next call's default alone. `compact` reads as `summary_only` on * a tool that declares only that. * * npm run build-pkg && node scripts/check-report-compact.mjs * * Runs against the BUILT dist/. */ import './test-isolation.mjs'; import assert from 'assert/strict'; import fs from 'fs'; import {after, test} from 'node:test'; import os from 'os'; import path from 'path'; import v8 from 'v8'; import {fileURLToPath, pathToFileURL} from 'url'; const here = path.dirname(fileURLToPath(import.meta.url)); const dist = path.join(here, '..', 'dist'); if (!fs.existsSync(path.join(dist, 'tools', 'round-digest.js'))) { console.error( 'check-report-compact: dist/tools/round-digest.js is missing; build first.', ); process.exit(1); } const load = rel => import(pathToFileURL(path.join(dist, rel)).href); const {z} = await import('zod'); const {translateAliases} = await load('param-aliases.js'); /** Registers one tool against a fake server; calls it with its defaults. */ async function toolFrom(file, register, name) { const mod = await load(file); let shape = null; let handler = null; mod[register]({ tool: (...a) => { if (a[0] === name) { shape = a.find((x, i) => i > 0 && x != null && typeof x === 'object'); handler = a.at(-1); } }, }); return async args => { const res = await handler(z.object(shape).parse(args), {}); return res.content.map(c => c.text).join('\n'); }; } const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'report-compact-')); after(() => fs.rmSync(tmp, {recursive: true, force: true})); function makeRound(name) { const dir = path.join(tmp, name); fs.mkdirSync(path.join(dir, 'analysis'), {recursive: true}); const rungs = [0, 50, 100].map((c, i) => ({ index: i, path: path.join(dir, `rung_0${i}_c${c}.heapsnapshot`), cycles: c, post_gc_heap_mb: 200 + i, })); fs.writeFileSync( path.join(dir, 'run.json'), JSON.stringify({ run_id: name, rungs, totals: {cycles: 100, ok: 100, fail: 0}, caveats: ['A caveat long enough to be worth hiding on a repeat call.'], }), ); fs.writeFileSync( path.join(dir, 'analysis', 'memlab_settle_check.txt'), [ '|Class|baseline|busy|settled|reclaimed|Verdict|', '|---|--:|--:|--:|--:|---|', ...Array.from( {length: 10}, (_, i) => `|Class${i}|1|${10 + i}|${5 + i}|5|held|`, ), '', ].join('\n'), ); return dir; } test('round_digest: a repeat call is compact and says so; compact:false is full', async () => { const digest = await toolFrom( 'tools/round-digest.js', 'registerRoundDigest', 'memlab_round_digest', ); const dir = makeRound('r1'); const first = await digest({run_dir: dir}); assert.doesNotMatch(first, /already described/); assert.match(first, /## Round caveats/); assert.match(first, /\|Class9\|/); const second = await digest({run_dir: dir}); assert.match(second, /_Compact: this run was already described/); assert.match(second, /\*\*Audit verdict: /); assert.match(second, /Cycles 100 · 3 rung\(s\) · post-GC 200\.0 → 202\.0 MB/); assert.match(second, /_1 round caveat\(s\); `compact: false` lists them\._/); assert.doesNotMatch(second, /A caveat long enough/); assert.match(second, /## Settle verdicts \(top 5\)/); assert.match(second, /\|Class4\|/); assert.doesNotMatch(second, /\|Class5\|/); assert.ok(second.length < first.length * 0.8, `${second.length}`); const full = await digest({run_dir: dir, compact: false}); assert.doesNotMatch(full, /already described/); assert.equal(full, first); }); test('round_digest: an explicit compact does not set the next default', async () => { const digest = await toolFrom( 'tools/round-digest.js', 'registerRoundDigest', 'memlab_round_digest', ); const dir = makeRound('r2'); const asked = await digest({run_dir: dir, compact: true}); assert.doesNotMatch(asked, /already described/); assert.match(asked, /round caveat\(s\); `compact: false`/); const next = await digest({run_dir: dir}); assert.match(next, /## Round caveats/); }); test('round_digest: a digest that failed does not make the retry compact', async () => { const digest = await toolFrom( 'tools/round-digest.js', 'registerRoundDigest', 'memlab_round_digest', ); const dir = makeRound('r3'); const settle = path.join(dir, 'analysis', 'memlab_settle_check.txt'); const text = fs.readFileSync(settle, 'utf8'); fs.rmSync(settle); fs.mkdirSync(settle); const failed = await digest({run_dir: dir}); assert.match(failed, /EISDIR|illegal operation on a directory/); fs.rmdirSync(settle); fs.writeFileSync(settle, text); const retry = await digest({run_dir: dir}); assert.doesNotMatch(retry, /already described/); assert.match(retry, /## Round caveats/); }); test('leak_report: the second report on the same rungs is compact', async () => { const report = await toolFrom( 'tools/leak-report.js', 'registerLeakReport', 'memlab_leak_report', ); globalThis.__reportCompactGrowth = []; const paths = [0, 1].map(i => { for (let k = 0; k < 2000; k++) { globalThis.__reportCompactGrowth.push({k}); } return v8.writeHeapSnapshot(path.join(tmp, `lr_${i}.heapsnapshot`)); }); const first = await report({paths}); assert.doesNotMatch(first, /already described/); const second = await report({paths}); assert.match(second, /^_Compact: this run was already described/m); assert.ok(second.length < first.length, `${second.length}`); const json = await report({paths, format: 'json'}); assert.doesNotMatch(json, /already described/); }); test('compact reads as summary_only on a tool that declares only that', () => { assert.deepEqual( translateAliases({compact: true}, {summary_only: 1, top: 1}).args, {summary_only: true}, ); assert.deepEqual(translateAliases({compact: true}, {compact: 1}).args, { compact: true, }); });