/** * 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 session-record upload status (upload-status.ts): the * line `memlab_server_status` and `memlab-cli doctor` show for the status * file an upload wrapper writes. * * npm run build-pkg && npm run test-units * * Runs against the BUILT dist/. */ import './test-isolation.mjs'; import assert from 'assert/strict'; import {spawnSync} from 'child_process'; import fs from 'fs'; import os from 'os'; import path from 'path'; import {test} from 'node:test'; 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, 'upload-status.js'))) { console.error('check-upload-status: dist/upload-status.js is missing.'); process.exit(1); } const load = rel => import(pathToFileURL(path.join(dist, rel)).href); const {readUploadStatus, uploadStatusLine, uploadStatusPath} = await load('upload-status.js'); const now = Date.parse('2026-11-26T12:00:00Z'); const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'upload-status-')); function statusFile(body) { const file = path.join(dir, `s${Math.random().toString(16).slice(2)}.json`); fs.writeFileSync(file, JSON.stringify(body)); return file; } const row = (round_key, status, error) => ({ round_key, label: round_key, status, ...(error != null ? {error} : {}), }); test('failed rounds: count, last error and fix', () => { const file = statusFile({ at: '2026-11-26T11:48:00Z', exit: 1, rounds: [ row('k1', 'created'), row('k2', 'failed', 'nothing was sent: token refused'), row('k3', 'failed', 'nothing was sent: token refused'), ], fix: 'set the token file', }); assert.equal( uploadStatusLine(readUploadStatus(file), now), '2 of 3 round(s) not recorded (last upload 12 min ago) — last error: nothing was sent: token refused (k3); fix: set the token file', ); }); test('all recorded, and nothing to record', () => { const all = statusFile({ at: '2026-11-26T09:00:00Z', exit: 0, rounds: [row('k1', 'created'), row('k2', 'unchanged')], }); assert.equal( uploadStatusLine(readUploadStatus(all), now), 'all 2 round(s) recorded (last upload 3 h ago)', ); const none = statusFile({at: '2026-11-26T11:59:00Z', exit: 0, rounds: []}); assert.match(uploadStatusLine(readUploadStatus(none), now), /^nothing to/); }); test('a command that failed without rows, with the generic fix', () => { const file = statusFile({ at: '2026-11-26T11:00:00Z', exit: 2, rounds: [], error: 'command not found', log: '/var/log/upload.log', }); assert.equal( uploadStatusLine(readUploadStatus(file), now), 'upload failed (last upload 1 h ago, exit 2) — last error: command not found; fix: run the upload again and read /var/log/upload.log', ); }); test('a skipped upload reads as not attempted, not as failed', () => { const file = statusFile({ at: '2026-11-26T11:57:00Z', exit: null, rounds: [], error: 'not attempted: an agent session cannot mint the token', fix: 'set the token file', skipped: 'agent-session', }); const status = readUploadStatus(file); assert.equal(status.skipped, 'agent-session'); const line = uploadStatusLine(status, now); assert.equal( line, 'upload not attempted 3 min ago: an agent session cannot mint the token; fix: set the token file', ); assert.doesNotMatch(line, /failed/); }); test('no file, or an unreadable one, shows nothing', () => { assert.equal(readUploadStatus(path.join(dir, 'absent.json')), null); const bad = path.join(dir, 'bad.json'); fs.writeFileSync(bad, '{not json'); assert.equal(readUploadStatus(bad), null); assert.equal(uploadStatusLine(null), null); }); test('the path comes from MEMLAB_UPLOAD_STATUS_FILE', () => { const before = process.env.MEMLAB_UPLOAD_STATUS_FILE; process.env.MEMLAB_UPLOAD_STATUS_FILE = '/x/status.json'; try { assert.equal(uploadStatusPath(), '/x/status.json'); delete process.env.MEMLAB_UPLOAD_STATUS_FILE; assert.equal( uploadStatusPath(), path.join(path.dirname(dist), 'upload-status.json'), ); } finally { if (before != null) process.env.MEMLAB_UPLOAD_STATUS_FILE = before; } }); const failing = () => statusFile({ at: new Date().toISOString(), exit: 1, rounds: [row('k1', 'failed', 'auth refused')], fix: 'renew the token', }); test('memlab_server_status shows the line', async () => { const before = process.env.MEMLAB_UPLOAD_STATUS_FILE; process.env.MEMLAB_UPLOAD_STATUS_FILE = failing(); try { let handler = null; const {registerServerStatus} = await load('tools/server-status.js'); registerServerStatus({tool: (...a) => (handler = a.at(-1))}); const r = await handler({}, {}); const text = r.content.map(c => c.text).join('\n'); assert.match( text, /- Session-record uploads: 1 of 1 round\(s\) not recorded .* last error: auth refused \(k1\); fix: renew the token/, ); } finally { if (before == null) delete process.env.MEMLAB_UPLOAD_STATUS_FILE; else process.env.MEMLAB_UPLOAD_STATUS_FILE = before; } }); test('memlab_server_status counts every eviction, not the kept log', async () => { const {recordEviction} = await load('perf-stats.js'); for (let i = 0; i < 25; i++) recordEviction(`h${i}`, `/r/e${i}`, 'cap'); let handler = null; const {registerServerStatus} = await load('tools/server-status.js'); registerServerStatus({tool: (...a) => (handler = a.at(-1))}); const text = (await handler({}, {})).content.map(c => c.text).join('\n'); assert.match(text, /; 25 snapshot\(s\) evicted \(last 20: h5 cap, h6 cap/); }); test('memlab-cli doctor reports the uploads', () => { const cli = path.join(here, '..', 'bin', 'memlab-cli.js'); const env = { ...process.env, MEMLAB_MCP_SERVER: path.join(dist, 'index.js'), MEMLAB_UPLOAD_STATUS_FILE: failing(), }; const res = spawnSync(process.execPath, [cli, 'doctor', '--json'], { env, encoding: 'utf8', }); const report = JSON.parse(res.stdout.trim().split('\n').at(-1)); assert.equal(report.uploads.failed, 1); assert.equal(report.uploads.rounds, 1); assert.equal(report.uploads.last_error, 'auth refused'); assert.equal(report.uploads.fix, 'renew the token'); assert.match(report.uploads.summary, /1 of 1 round\(s\) not recorded/); });