/** * 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 `evalErrorHint` (eval-hints.ts): the error messages seen in * agent transcripts each get their fix. * * 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 built = path.join(here, '..', 'dist', 'eval-hints.js'); if (!fs.existsSync(built)) { console.error( 'check-eval-error-hints: dist/eval-hints.js is missing; build first.', ); process.exit(1); } const hints = await import(pathToFileURL(built).href); const {evalErrorHint} = hints; const cases = [ [ 'TypeError: byField[e.name_or_index].push is not a function', /Object\.prototype/, ], [ "TypeError: Cannot read properties of undefined (reading 'push')", /initialise/, ], [ "TypeError: Cannot read properties of null (reading 'references')", /not in THIS snapshot/, ], [ "TypeError: Cannot read properties of null (reading 'id')", /not in THIS snapshot/, ], [ "TypeError: Cannot read properties of undefined (reading 'fromNode')", /past the end/, ], ['TypeError: ids.filter is not a function', /not an array/], ['TypeError: snapshot.nodes.filter is not a function', /nodes\.forEach/], [ "TypeError: Cannot read properties of undefined (reading 'add')", /g = new Set\(\)\)\); g\.add\(x\)/, ], [ "TypeError: Cannot read properties of undefined (reading 'set')", /g = new Map\(\)\)\); g\.set\(k, x\)/, ], [ "TypeError: Cannot read properties of undefined (reading 'push')", /Object\.create\(null\)/, ], ['TypeError: Do not know how to serialize a BigInt', /decimal string/], [ 'TypeError: (x.value.name || x.value).toString is not a function', /String\(x\)/, ], ]; for (const [message, want] of cases) { test(message, () => { assert.match(evalErrorHint(message) ?? '', want); }); } test('an unrelated error gets no hint', () => { assert.equal(evalErrorHint('RangeError: Invalid array length'), null); }); test('an undefined `.type` is not blamed on edge indexing', () => { assert.equal( evalErrorHint( "TypeError: Cannot read properties of undefined (reading 'type')", ), null, ); }); test('the snapshot.nodes hint does not suggest Array.from', () => { assert.doesNotMatch( evalErrorHint('TypeError: snapshot.nodes.map is not a function') ?? '', /Wrap it with `Array\.from/, ); }); test('"is not iterable" names the value that is not', () => { const {notIterableHint} = hints; assert.equal(notIterableHint('TypeError: x.y is not a function'), null); assert.match( notIterableHint('TypeError: helpers.elements(...) is not iterable'), /^`helpers\.elements\(\.\.\.\)` is not a list\. .*info object/, ); assert.match( notIterableHint( 'undefined is not iterable (cannot read property Symbol(Symbol.iterator))', ), /^`undefined` is not a list\. The value was `undefined`/, ); assert.match( notIterableHint('TypeError: node.id is not iterable'), /node id: resolve it with `snapshot\.getNodeById/, ); assert.match( notIterableHint('TypeError: number 5 is not iterable'), /It is a number/, ); assert.match( notIterableHint('TypeError: 5 is not iterable'), /It is a number/, ); assert.doesNotMatch( notIterableHint('TypeError: numbersByClass is not iterable'), /It is a number/, ); assert.match( notIterableHint('TypeError: n.props is not iterable'), /Object\.entries/, ); assert.match( notIterableHint('TypeError: rows is not iterable'), /^`rows` is not a list\. .*helpers\.nodes\(\)/, ); // V8 names the expression, not the value: an undefined `node.refs` reads // like an object, so the default must not say which it is. const refs = notIterableHint('TypeError: node.refs is not iterable'); assert.match(refs, /may be `undefined` \(a property that is not there/); assert.doesNotMatch(refs, /It is an object/); assert.match( notIterableHint( 'TypeError: object null is not iterable (cannot read property Symbol(Symbol.iterator))', ), /^`object null` is not a list\. The value was `null`/, ); });