import { test } from 'node:test' import assert from 'node:assert' import transform from '../lib/transform.js' test('converts a classic choo v7 header to v8 ESM', () => { const src = [ "var choo = require('choo')", "var html = require('choo/html')", "var devtools = require('choo-devtools')", '', 'var app = choo()', "app.use(devtools())", 'module.exports = app' ].join('\n') const { code, changed, notes } = transform(src) assert.ok(changed) assert.match(code, /import choo from '@uhhm\/buuh'/) assert.match(code, /import html from '@uhhm\/buuh-html'/) assert.match(code, /import devtools from '@uhhm\/buuh-devtools'/) assert.match(code, /export default app/) assert.deepStrictEqual(notes, []) }) test('destructured and property requires', () => { const src = [ "const { render } = require('some-lib')", "const thing = require('other-lib').thing", "const dflt = require('third-lib').default", "require('./side-effect')" ].join('\n') const { code } = transform(src) assert.match(code, /import { render } from 'some-lib'/) assert.match(code, /import { thing as thing } from 'other-lib'/) assert.match(code, /import dflt from 'third-lib'/) assert.match(code, /import '\.\/side-effect'/) }) test('already-ESM sources get specifiers remapped', () => { const src = [ "import choo from 'choo'", "import html from 'nanohtml'", "import raw from 'nanohtml/raw'", "import morph from 'nanomorph'", "import Component from 'nanocomponent'" ].join('\n') const { code } = transform(src) assert.match(code, /from '@uhhm\/buuh'/) assert.match(code, /from '@uhhm\/buuh-html'\n/) assert.match(code, /from '@uhhm\/buuh-html\/raw'/) assert.match(code, /from '@uhhm\/buuh-html\/morph'/) assert.match(code, /from '@uhhm\/buuh-component'/) }) test('retired packages produce notes, not rewrites', () => { const src = [ "var nanobus = require('nanobus')", "var lazyRoute = require('choo-lazy-route')" ].join('\n') const { code, notes } = transform(src) assert.match(code, /from 'nanobus'/, 'retired specifier left for the human') assert.ok(notes.some((n) => n.includes('nanobus'))) assert.ok(notes.some((n) => n.includes('lazy()')), 'choo-lazy-route points at lazy()') }) test('dynamic import() specifiers are remapped too', () => { const { code } = transform("const mod = await import('choo/html')") assert.match(code, /import\('@uhhm\/buuh-html'\)/) }) test('unconvertible CJS is reported honestly', () => { const src = [ "module.exports.helper = function () {}", "function f () { const x = require('choo') }" ].join('\n') const { code, notes } = transform(src) assert.match(code, /require\('@uhhm\/buuh'\)/, 'specifier remapped even inside functions') assert.ok(notes.some((n) => n.includes('module.exports'))) assert.ok(notes.some((n) => n.includes('require() calls remain'))) }) test('non-choo sources pass through untouched', () => { const src = "import fs from 'node:fs'\nexport const x = 1\n" const { changed, notes } = transform(src) assert.strictEqual(changed, false) assert.deepStrictEqual(notes, []) })