|
| 1 | +import fs from 'node:fs/promises' |
| 2 | +import path from 'node:path' |
| 3 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 4 | +import { getExtFromUrl, isUrl, readSource, run } from '../src/cli' |
| 5 | + |
| 6 | +describe('isUrl', () => { |
| 7 | + it('valid URL', () => { |
| 8 | + expect(isUrl('http://localhost:3000/file.ts')).toBe(true) |
| 9 | + expect(isUrl('https://raw.githubusercontent.com/shikijs/shiki/refs/heads/main/taze.config.ts')).toBe(true) |
| 10 | + expect(isUrl('/absolute/path/file.ts')).toBe(false) |
| 11 | + expect(isUrl('relative/path/file.ts')).toBe(false) |
| 12 | + expect(isUrl('file.ts')).toBe(false) |
| 13 | + }) |
| 14 | +}) |
| 15 | + |
| 16 | +describe('getExtFromUrl', () => { |
| 17 | + it('extracts extension', () => { |
| 18 | + expect(getExtFromUrl('https://example.com/file.ts')).toBe('ts') |
| 19 | + expect(getExtFromUrl('https://shiki.style/guide.html')).toBe('html') |
| 20 | + }) |
| 21 | + |
| 22 | + it('handles query params', () => { |
| 23 | + expect(getExtFromUrl('https://github.com/shikijs/shiki/blob/main/taze.config.ts?raw=true')).toBe('ts') |
| 24 | + }) |
| 25 | + |
| 26 | + it('invalid URL', () => { |
| 27 | + expect(getExtFromUrl('not-a-url')).toBe('') |
| 28 | + }) |
| 29 | +}) |
| 30 | + |
| 31 | +describe('readSource', () => { |
| 32 | + const testDir = path.join(import.meta.dirname, '__fixtures__') |
| 33 | + const testFile = path.join(testDir, 'test.ts') |
| 34 | + const testContent = 'const x: number = 1' |
| 35 | + |
| 36 | + beforeEach(async () => { |
| 37 | + await fs.mkdir(testDir, { recursive: true }) |
| 38 | + await fs.writeFile(testFile, testContent) |
| 39 | + }) |
| 40 | + |
| 41 | + afterEach(async () => { |
| 42 | + await fs.rm(testDir, { recursive: true, force: true }) |
| 43 | + }) |
| 44 | + |
| 45 | + it('local file', async () => { |
| 46 | + const result = await readSource(testFile) |
| 47 | + expect(result.content).toBe(testContent) |
| 48 | + expect(result.ext).toBe('ts') |
| 49 | + }) |
| 50 | + |
| 51 | + it('remote URL', async () => { |
| 52 | + const mockContent = 'export const foo = "bar"' |
| 53 | + vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ |
| 54 | + ok: true, |
| 55 | + text: () => Promise.resolve(mockContent), |
| 56 | + })) |
| 57 | + |
| 58 | + const result = await readSource('https://example.com/file.js') |
| 59 | + expect(result.content).toBe(mockContent) |
| 60 | + expect(result.ext).toBe('js') |
| 61 | + |
| 62 | + vi.unstubAllGlobals() |
| 63 | + }) |
| 64 | + |
| 65 | + it('failed fetch', async () => { |
| 66 | + vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ |
| 67 | + ok: false, |
| 68 | + status: 404, |
| 69 | + statusText: 'Not Found', |
| 70 | + })) |
| 71 | + |
| 72 | + await expect(readSource('https://example.com/missing.js')) |
| 73 | + .rejects |
| 74 | + .toThrowError('Failed to fetch https://example.com/missing.js: 404 Not Found') |
| 75 | + |
| 76 | + vi.unstubAllGlobals() |
| 77 | + }) |
| 78 | +}) |
| 79 | + |
| 80 | +describe('run', () => { |
| 81 | + const testDir = path.join(import.meta.dirname, '__fixtures__') |
| 82 | + const testFile = path.join(testDir, 'sample.ts') |
| 83 | + |
| 84 | + beforeEach(async () => { |
| 85 | + await fs.mkdir(testDir, { recursive: true }) |
| 86 | + await fs.writeFile(testFile, 'const x = 1') |
| 87 | + }) |
| 88 | + |
| 89 | + afterEach(async () => { |
| 90 | + await fs.rm(testDir, { recursive: true, force: true }) |
| 91 | + }) |
| 92 | + |
| 93 | + it('local file', async () => { |
| 94 | + const output: string[] = [] |
| 95 | + await run(['node', 'shiki', testFile], msg => output.push(msg)) |
| 96 | + |
| 97 | + expect(output.length).toBe(1) |
| 98 | + expect(output[0]).toContain('const') |
| 99 | + }) |
| 100 | + |
| 101 | + it('remote URL', async () => { |
| 102 | + vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ |
| 103 | + ok: true, |
| 104 | + text: () => Promise.resolve('const y = 2'), |
| 105 | + })) |
| 106 | + |
| 107 | + const output: string[] = [] |
| 108 | + await run(['node', 'shiki', 'https://example.com/code.ts'], msg => output.push(msg)) |
| 109 | + |
| 110 | + expect(output.length).toBe(1) |
| 111 | + expect(output[0]).toContain('const') |
| 112 | + |
| 113 | + vi.unstubAllGlobals() |
| 114 | + }) |
| 115 | + |
| 116 | + it('--lang option', async () => { |
| 117 | + vi.stubGlobal('fetch', vi.fn().mockResolvedValue({ |
| 118 | + ok: true, |
| 119 | + text: () => Promise.resolve('print("hello")'), |
| 120 | + })) |
| 121 | + |
| 122 | + const output: string[] = [] |
| 123 | + await run(['node', 'shiki', '--lang', 'python', 'https://example.com/code'], msg => output.push(msg)) |
| 124 | + |
| 125 | + expect(output.length).toBe(1) |
| 126 | + expect(output[0]).toContain('print') |
| 127 | + |
| 128 | + vi.unstubAllGlobals() |
| 129 | + }) |
| 130 | +}) |
0 commit comments