tricu

An interpreted language for exploring Tree Calculus
Log | Files | Refs | README | LICENSE

import-picker.test.mjs (2442B)


      1 import test from 'node:test';
      2 import assert from 'node:assert/strict';
      3 import {importPlan} from '../dist/import-plan.js';
      4 import {libraryIndex,latestLibraries,matchesLibrary} from '../dist/library-index.js';
      5 const manifest='a'.repeat(64),other='b'.repeat(64);
      6 const stage=(imports=[],body='')=>({imports,body,exports:[['*','']],native:true});
      7 test('import insertion is a prefix edit; source and Unicode comments remain intact',()=>{
      8  const source='-- 🐕 comment\n!export answer\nanswer = 42';
      9  assert.deepEqual(importPlan(source,stage(),manifest,'N'),{source:`!import ${manifest} as N\n${source}`,statement:`!import ${manifest} as N\n`});
     10 });
     11 test('native header determines duplicate identity and namespace conflicts',()=>{
     12  const source=`-- 🐕\n!import ${manifest} as Old\n!export answer\nanswer = 42`;
     13  const plan=importPlan(source,stage([[manifest,'Old']],'-- 🐕\n \n \nanswer = 42'),manifest,'N');
     14  assert.equal(plan.source,source);assert.equal(plan.existing.alias,'Old');assert.equal(source.slice(plan.existing.start,plan.existing.end),`!import ${manifest} as Old`);
     15  assert.throws(()=>importPlan(source,stage([[other,'N']]),manifest,'N'),/already used/);
     16  assert.equal(importPlan(source,stage([[manifest,'*']]),manifest,'N').existing.alias,'*');
     17 });
     18 test('insertion validates namespace, manifest and resulting source bounds',()=>{
     19  for(const alias of ['','*','A.B','x\n!export *','1name','x'.repeat(129)])assert.throws(()=>importPlan('!export x\nx=1',stage(),manifest,alias),/namespace/i);
     20  assert.throws(()=>importPlan('x=1',stage(),'deadbeef','N'),/manifest/);
     21  assert.throws(()=>importPlan('x'.repeat(16384),stage(),manifest,'N'),/limit/);
     22 });
     23 const row=version=>({name:'tricu-lib-test',version,hash:other,manifest,title:'test',description:'Natural arithmetic',names:['add','multiply'],alias:'N',reference:'libraries/tricu-lib-test-'+version+'.md'});
     24 test('library metadata is bounded and latest versions use release precedence',()=>{
     25  const rows=libraryIndex({schema:1,releases:[row('0.9.0'),row('0.10.0'),row('0.10.0-rc.1')]});
     26  assert.equal(latestLibraries(rows)[0].version,'0.10.0');
     27  assert(matchesLibrary(rows[0],'arithmetic multiply'));assert(!matchesLibrary(rows[0],'socket'));
     28  for(const change of [{reference:'https://example.com/a.md'},{reference:'libraries/../../a.md'},{alias:'*'},{names:['a'.repeat(513)]},{description:[]}] )assert.throws(()=>libraryIndex({schema:1,releases:[{...row('1.0.0'),...change}]}));
     29 });