|
| 1 | +import { afterAll, beforeAll, describe, expect, it } from 'bun:test' |
| 2 | +import { existsSync } from 'node:fs' |
| 3 | +import { mkdir, readFile, rm, writeFile } from 'node:fs/promises' |
| 4 | +import { join } from 'node:path' |
| 5 | +import { process } from '../src/core' |
| 6 | +import { processSvg } from '../src/processor' |
| 7 | + |
| 8 | +const FIXTURES_DIR = join(import.meta.dir, 'fixtures') |
| 9 | +const OUTPUT_DIR = join(FIXTURES_DIR, 'output', 'svg-tests') |
| 10 | +const TEST_SVG = join(FIXTURES_DIR, 'stacks-logo.svg') |
| 11 | + |
| 12 | +describe('SVG Processing', () => { |
| 13 | + beforeAll(async () => { |
| 14 | + await mkdir(OUTPUT_DIR, { recursive: true }) |
| 15 | + }) |
| 16 | + |
| 17 | + afterAll(async () => { |
| 18 | + await rm(OUTPUT_DIR, { recursive: true, force: true }) |
| 19 | + }) |
| 20 | + |
| 21 | + it('should optimize SVG through processSvg', async () => { |
| 22 | + const output = join(OUTPUT_DIR, 'optimized-logo.svg') |
| 23 | + const result = await processSvg({ |
| 24 | + input: TEST_SVG, |
| 25 | + output, |
| 26 | + prettify: false, |
| 27 | + }) |
| 28 | + |
| 29 | + // Verify the file exists |
| 30 | + expect(existsSync(output)).toBe(true) |
| 31 | + |
| 32 | + // Verify optimization results |
| 33 | + expect(result.inputSize).toBeGreaterThan(0) |
| 34 | + expect(result.outputSize).toBeGreaterThan(0) |
| 35 | + expect(result.saved).toBeGreaterThanOrEqual(0) // It might already be optimized |
| 36 | + expect(result.savedPercentage).toBeGreaterThanOrEqual(0) |
| 37 | + |
| 38 | + // Basic content verification |
| 39 | + const content = await readFile(output, 'utf-8') |
| 40 | + expect(content).toContain('<svg') |
| 41 | + expect(content).toContain('</svg>') |
| 42 | + }) |
| 43 | + |
| 44 | + it('should optimize SVG through core process', async () => { |
| 45 | + const output = join(OUTPUT_DIR, 'core-processed-logo.svg') |
| 46 | + const result = await process({ |
| 47 | + input: TEST_SVG, |
| 48 | + output, |
| 49 | + }) |
| 50 | + |
| 51 | + // Verify the file exists |
| 52 | + expect(existsSync(output)).toBe(true) |
| 53 | + |
| 54 | + // Verify it used the SVG processor |
| 55 | + expect(result.inputPath).toBe(TEST_SVG) |
| 56 | + expect(result.outputPath).toBe(output) |
| 57 | + }) |
| 58 | + |
| 59 | + it('should handle SVG with excess whitespace and comments', async () => { |
| 60 | + // Create a test SVG with excess whitespace and comments |
| 61 | + const testSvg = ` |
| 62 | + <svg xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 100 100"> |
| 63 | + <!-- This is a comment that should be removed --> |
| 64 | +
|
| 65 | + <rect |
| 66 | + x="10" |
| 67 | + y="10" |
| 68 | + width="80" |
| 69 | + height="80" |
| 70 | + fill="blue" |
| 71 | + /> |
| 72 | +
|
| 73 | + <!-- Another comment --> |
| 74 | + <circle cx="50" cy="50" r="30" fill="red" /> |
| 75 | + </svg> |
| 76 | + ` |
| 77 | + |
| 78 | + const inputFile = join(OUTPUT_DIR, 'whitespace-test.svg') |
| 79 | + const outputFile = join(OUTPUT_DIR, 'whitespace-test-optimized.svg') |
| 80 | + |
| 81 | + await writeFile(inputFile, testSvg) |
| 82 | + |
| 83 | + const result = await processSvg({ |
| 84 | + input: inputFile, |
| 85 | + output: outputFile, |
| 86 | + prettify: false, |
| 87 | + }) |
| 88 | + |
| 89 | + // Verify optimization |
| 90 | + expect(result.saved).toBeGreaterThan(0) |
| 91 | + |
| 92 | + // Check content |
| 93 | + const content = await readFile(outputFile, 'utf-8') |
| 94 | + expect(content.length).toBeLessThan(testSvg.length) |
| 95 | + |
| 96 | + // With current SVGO setup, we may not be able to control all options |
| 97 | + // so we'll just check general optimization occurred |
| 98 | + expect(content).toContain('<svg') |
| 99 | + // SVGO might convert rect to path, so check for either |
| 100 | + expect(content.includes('<rect') || content.includes('<path')).toBe(true) |
| 101 | + expect(content).toContain('<circle') |
| 102 | + }) |
| 103 | + |
| 104 | + it('should optimize SVG with width and height attributes', async () => { |
| 105 | + const output = join(OUTPUT_DIR, 'with-dimensions.svg') |
| 106 | + |
| 107 | + await processSvg({ |
| 108 | + input: TEST_SVG, |
| 109 | + output, |
| 110 | + }) |
| 111 | + |
| 112 | + const content = await readFile(output, 'utf-8') |
| 113 | + |
| 114 | + // Using the default SVGO preset, SVG should have width and height |
| 115 | + expect(content).toMatch(/width=["']\d+["']/) |
| 116 | + expect(content).toMatch(/height=["']\d+["']/) |
| 117 | + }) |
| 118 | + |
| 119 | + it('should optimize SVG and produce valid output', async () => { |
| 120 | + const output = join(OUTPUT_DIR, 'valid-svg.svg') |
| 121 | + |
| 122 | + await processSvg({ |
| 123 | + input: TEST_SVG, |
| 124 | + output, |
| 125 | + }) |
| 126 | + |
| 127 | + const content = await readFile(output, 'utf-8') |
| 128 | + |
| 129 | + // The output should be valid SVG |
| 130 | + expect(content).toMatch(/<svg[^>]*>/) |
| 131 | + expect(content).toMatch(/<\/svg>/) |
| 132 | + expect(content).toContain('xmlns="http://www.w3.org/2000/svg"') |
| 133 | + }) |
| 134 | +}) |
0 commit comments