/** * 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 fix-status lookup (fix-status.ts): JSON output reads as * its fields, anything else as its first line. * * npm run build-pkg && npm run test-units * * Runs against the BUILT dist/. */ import './test-isolation.mjs'; import assert from 'assert/strict'; import fs from 'fs'; 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, 'fix-status.js'))) { console.error('check-fix-status: dist/fix-status.js is missing.'); process.exit(1); } const {fixStatusText, fixStatusLinesFor} = await import( pathToFileURL(path.join(dist, 'fix-status.js')).href ); test('JSON output reads as its fields, not its first line', () => { assert.equal( fixStatusText('{\n "status": "Unpublished",\n "is_landed": false\n}\n'), 'status=Unpublished, is_landed=false', ); assert.equal( fixStatusText('[{"status": "Closed", "landed_at": null}]'), 'status=Closed, landed_at=null', ); assert.equal( fixStatusText('{"status": "Accepted", "reviewers": ["a", "b"]}'), 'status=Accepted, reviewers=["a","b"]', ); assert.equal(fixStatusText('{}'), null); const long = fixStatusText( JSON.stringify({ summary: `line one\nline two ${'x'.repeat(300)}`, status: 'Closed', }), ); assert.doesNotMatch(long, /\n/); assert.match(long, /^summary=line one line two x+…, status=Closed$/); assert.equal(fixStatusText(''), null); }); test('other output reads as its first line', () => { assert.equal(fixStatusText('Landed\nmore detail\n'), 'Landed'); assert.equal(fixStatusText('{not json\nsecond'), '{not json'); }); test('the lookup prints a pretty-printed JSON answer on one line', async () => { const before = process.env.MEMLAB_FIX_STATUS_CMD; process.env.MEMLAB_FIX_STATUS_CMD = 'printf \'{\\n "status": "Unpublished",\\n "id": "{diff}"\\n}\\n\''; try { assert.deepEqual(await fixStatusLinesFor(['D901']), [ '- Fix status of D901: status=Unpublished, id=D901', ]); } finally { if (before == null) delete process.env.MEMLAB_FIX_STATUS_CMD; else process.env.MEMLAB_FIX_STATUS_CMD = before; } });