main
  1// Copyright 2024 Google LLC
  2//
  3// Licensed under the Apache License, Version 2.0 (the "License");
  4// you may not use this file except in compliance with the License.
  5// You may obtain a copy of the License at
  6//
  7//     https://www.apache.org/licenses/LICENSE-2.0
  8//
  9// Unless required by applicable law or agreed to in writing, software
 10// distributed under the License is distributed on an "AS IS" BASIS,
 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12// See the License for the specific language governing permissions and
 13// limitations under the License.
 14
 15import assert from 'node:assert'
 16import { test, describe } from 'node:test'
 17import {
 18  YAML,
 19  MAML,
 20  minimist,
 21  which,
 22  glob,
 23  nodeFetch as fetch,
 24} from '../build/vendor.cjs'
 25
 26describe('vendor API', () => {
 27  describe('YAML', () => {
 28    test('parse()', () => {
 29      assert.deepEqual(YAML.parse('a: b\n'), { a: 'b' })
 30    })
 31    test('stringify()', () => {
 32      assert.equal(YAML.stringify({ a: 'b' }), 'a: b\n')
 33    })
 34  })
 35
 36  describe('MAML', () => {
 37    test('parse()/stringify()', () => {
 38      const maml = `{
 39  project: "MAML"
 40  tags: [
 41    "minimal"
 42    "readable"
 43  ]
 44  spec: {
 45    version: 1
 46    author: "Anton Medvedev"
 47  }
 48  notes: """
 49This is a multiline string.
 50Keeps formatting as‑is.
 51"""
 52}`
 53      const obj = MAML.parse(maml)
 54
 55      assert.deepEqual(MAML.parse(MAML.stringify(obj)), obj)
 56      assert.deepEqual(obj, {
 57        project: 'MAML',
 58        tags: ['minimal', 'readable'],
 59        spec: {
 60          version: 1,
 61          author: 'Anton Medvedev',
 62        },
 63        notes: 'This is a multiline string.\nKeeps formatting as‑is.\n',
 64      })
 65    })
 66  })
 67
 68  test('globby() works', async () => {
 69    assert.deepEqual(await glob('*.md'), ['README.md'])
 70    assert.deepEqual(glob.sync('*.md'), ['README.md'])
 71  })
 72
 73  test('fetch() works', async () => {
 74    assert.match(
 75      await fetch('https://example.com').then((res) => res.text()),
 76      /Example Domain/
 77    )
 78  })
 79
 80  test('which() available', async () => {
 81    assert.equal(which.sync('npm'), await which('npm'))
 82    assert.throws(() => which.sync('not-found-cmd'), /not-found-cmd/)
 83  })
 84
 85  test('minimist available', async () => {
 86    assert.equal(typeof minimist, 'function')
 87  })
 88
 89  test('minimist works', async () => {
 90    assert.deepEqual(
 91      minimist(
 92        ['--foo', 'bar', '-a', '5', '-a', '42', '--force', './some.file'],
 93        { boolean: 'force' }
 94      ),
 95      {
 96        a: [5, 42],
 97        foo: 'bar',
 98        force: true,
 99        _: ['./some.file'],
100      }
101    )
102  })
103})