93 lines
4.7 KiB
JavaScript
93 lines
4.7 KiB
JavaScript
// The variable tree's data: paging of big arrays, reading a whole value, its copy forms.
|
|
// Run: docker run --rm -v $PWD:/w -w /w node:22-alpine node --test ide/ui/js/var-model.test.mjs
|
|
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { pagePlan, fetchTree, toJson, toPhp, toData, phpString, editText, isEditable, valueText } from './var-model.js';
|
|
|
|
test('paging: how many are left and which page comes next', () => {
|
|
assert.deepEqual(pagePlan(32, 100, 32), { left: 68, page: 1 });
|
|
assert.deepEqual(pagePlan(64, 100, 32), { left: 36, page: 2 });
|
|
assert.deepEqual(pagePlan(100, 100, 32), { left: 0, page: null });
|
|
assert.deepEqual(pagePlan(10, 10, 0), { left: 0, page: null });
|
|
assert.deepEqual(pagePlan(5, 9, 0), { left: 4, page: null }, 'unknown page size: no next page to ask for');
|
|
});
|
|
|
|
/** A fake debugger: an array of `n` ints that answers page by page. */
|
|
const bigArray = (n, pagesize = 32) => {
|
|
const asked = [];
|
|
const fetch = async (v, page) => {
|
|
asked.push([v.fullname, page]);
|
|
if (v.fullname !== '$big') return { items: [], size: 0, pagesize };
|
|
const items = [];
|
|
for (let i = page * pagesize; i < Math.min(n, (page + 1) * pagesize); i++) items.push({ name: String(i), fullname: `$big[${i}]`, type: 'int', value: String(i * 2), children: false });
|
|
return { items, size: n, pagesize };
|
|
};
|
|
return { fetch, asked, root: { name: '$big', fullname: '$big', type: 'array', value: `array(${n})`, children: true, size: n, pagesize } };
|
|
};
|
|
|
|
test('reading a whole big array pages through it', async () => {
|
|
const { fetch, asked, root } = bigArray(100);
|
|
const { tree, cut } = await fetchTree(fetch, root);
|
|
assert.equal(cut, false);
|
|
assert.equal(tree.items.length, 100);
|
|
assert.deepEqual(asked.map((a) => a[1]), [0, 1, 2, 3]);
|
|
assert.equal(tree.items[99].value, '198');
|
|
});
|
|
|
|
test('reading stops at the budget and says so', async () => {
|
|
const { fetch, root } = bigArray(100);
|
|
const { tree, cut } = await fetchTree(fetch, root, { maxNodes: 40 });
|
|
assert.equal(cut, true);
|
|
assert.ok(tree.items.length <= 64 && tree.items.length >= 32, `${tree.items.length} items`);
|
|
});
|
|
|
|
test('reading stops at the depth', async () => {
|
|
const deep = { name: '$a', fullname: '$a', type: 'array', value: 'array(1)', children: true, size: 1, pagesize: 32 };
|
|
const fetch = async () => ({ items: [{ ...deep }], size: 1, pagesize: 32 });
|
|
const { tree, cut } = await fetchTree(fetch, deep, { maxDepth: 3 });
|
|
assert.equal(cut, true);
|
|
let d = 0;
|
|
for (let n = tree; n.items.length; n = n.items[0]) d++;
|
|
assert.equal(d, 3);
|
|
});
|
|
|
|
const hero = {
|
|
name: '$hero', type: 'object', class: 'App\\Hero', children: true, items: [
|
|
{ name: 'name', type: 'string', value: "Ada's", children: false },
|
|
{ name: 'lives', type: 'int', value: '3', children: false },
|
|
{ name: 'alive', type: 'bool', value: 'true', children: false },
|
|
{ name: 'boss', type: 'null', value: 'null', children: false },
|
|
{ name: 'score', type: 'float', value: '1.5', children: false },
|
|
{ name: 'items', type: 'array', children: true, items: [{ name: '0', type: 'string', value: 'sword' }, { name: '1', type: 'string', value: 'bow' }] },
|
|
{ name: 'stats', type: 'array', children: true, items: [{ name: 'hp', type: 'int', value: '9' }, { name: '7', type: 'int', value: '1' }] },
|
|
],
|
|
};
|
|
|
|
test('JSON: lists stay lists, maps stay maps, scalars keep their type', () => {
|
|
assert.deepEqual(toData(hero), { name: "Ada's", lives: 3, alive: true, boss: null, score: 1.5, items: ['sword', 'bow'], stats: { hp: 9, 7: 1 } });
|
|
assert.equal(JSON.parse(toJson(hero)).items[1], 'bow');
|
|
});
|
|
|
|
test('PHP code like var_export', () => {
|
|
const php = toPhp(hero);
|
|
assert.match(php, /^\\App\\Hero::__set_state\(\[\n/);
|
|
assert.match(php, /'name' => 'Ada\\'s',/);
|
|
assert.match(php, /'lives' => 3,/);
|
|
assert.match(php, /'alive' => true,/);
|
|
assert.match(php, /'boss' => null,/);
|
|
assert.match(php, /'items' => \[\n {8}'sword',\n {8}'bow',\n {4}\],/);
|
|
assert.match(php, /'stats' => \[\n {8}'hp' => 9,\n {8}7 => 1,\n {4}\],/);
|
|
assert.equal(toPhp({ type: 'array', items: [] }), '[]');
|
|
});
|
|
|
|
test('editing text and literals', () => {
|
|
assert.equal(phpString("it's \\ here"), "'it\\'s \\\\ here'");
|
|
assert.equal(editText({ type: 'string', value: 'Ada' }), "'Ada'");
|
|
assert.equal(editText({ type: 'string', value: 'cut…', full: 'the whole text' }), "'the whole text'");
|
|
assert.equal(editText({ type: 'null', value: 'null' }), 'null');
|
|
assert.equal(editText({ type: 'bool', value: 'true' }), 'true');
|
|
assert.equal(isEditable({ type: 'int', fullname: '$a' }), true);
|
|
assert.equal(isEditable({ type: 'array', fullname: '$a' }), false);
|
|
assert.equal(isEditable({ type: 'int' }), false);
|
|
assert.equal(valueText({ type: 'string', value: 'a"b' }), '"a\\"b"');
|
|
});
|