summaryrefslogtreecommitdiff
path: root/packages/vercel-optimize-tests/test/large-static-asset.test.mjs
blob: d22412bb7caa137b8d8afbd120497fc3ce882d3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// fs.stat-based scanner — tests use tmp dirs because the lib walks public/ directly.

import { test } from 'node:test';
import assert from 'node:assert/strict';
import { mkdtemp, mkdir, writeFile, rm } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { scan, metadata } from '../../../skills/vercel-optimize/lib/scanners/large-static-asset.mjs';

async function withTmpProject(fn) {
  const root = await mkdtemp(join(tmpdir(), 'vo-large-asset-'));
  try {
    await mkdir(join(root, 'public'), { recursive: true });
    await fn(root);
  } finally {
    await rm(root, { recursive: true, force: true });
  }
}

test('large-static-asset: flags files >=500KB', async () => {
  await withTmpProject(async (root) => {
    await writeFile(join(root, 'public', 'hero.mp4'), Buffer.alloc(2_000_000));
    await writeFile(join(root, 'public', 'small.png'), Buffer.alloc(100_000));
    const findings = await scan({ rootDir: root });
    assert.equal(findings.length, 1);
    assert.equal(findings[0].pattern, 'large-static-asset');
    assert.match(findings[0].file, /public\/hero\.mp4$/);
    assert.equal(findings[0].sizeBytes, 2_000_000);
    assert.match(findings[0].evidence, /2\.00 MB/);
  });
});

test('large-static-asset: silent on assets below 500KB', async () => {
  await withTmpProject(async (root) => {
    await writeFile(join(root, 'public', 'logo.svg'), Buffer.alloc(50_000));
    const findings = await scan({ rootDir: root });
    assert.equal(findings.length, 0);
  });
});

test('large-static-asset: skips small-but-noisy extensions (ico, manifest, html, txt, xml, json)', async () => {
  await withTmpProject(async (root) => {
    await writeFile(join(root, 'public', 'favicon.ico'), Buffer.alloc(1_000_000));
    await writeFile(join(root, 'public', 'sitemap.xml'), Buffer.alloc(1_000_000));
    await writeFile(join(root, 'public', 'robots.txt'), Buffer.alloc(1_000_000));
    await writeFile(join(root, 'public', 'manifest.webmanifest'), Buffer.alloc(1_000_000));
    await writeFile(join(root, 'public', 'something.json'), Buffer.alloc(1_000_000));
    const findings = await scan({ rootDir: root });
    assert.equal(findings.length, 0);
  });
});

test('large-static-asset: skips .well-known/', async () => {
  await withTmpProject(async (root) => {
    await mkdir(join(root, 'public', '.well-known'), { recursive: true });
    await writeFile(join(root, 'public', '.well-known', 'apple-developer-merchantid.txt'), Buffer.alloc(2_000_000));
    const findings = await scan({ rootDir: root });
    assert.equal(findings.length, 0);
  });
});

test('large-static-asset: caps at TOP_N=20 and sorts by size desc', async () => {
  await withTmpProject(async (root) => {
    for (let i = 0; i < 25; i++) {
      await writeFile(join(root, 'public', `vid${i}.mp4`), Buffer.alloc(600_000 + i * 1000));
    }
    const findings = await scan({ rootDir: root });
    assert.equal(findings.length, 20);
    for (let i = 0; i < findings.length - 1; i++) {
      assert.ok(findings[i].sizeBytes >= findings[i + 1].sizeBytes, 'sorted desc');
    }
  });
});

test('large-static-asset: walks nested directories', async () => {
  await withTmpProject(async (root) => {
    await mkdir(join(root, 'public', 'videos', '2024'), { recursive: true });
    await writeFile(join(root, 'public', 'videos', '2024', 'intro.mp4'), Buffer.alloc(800_000));
    const findings = await scan({ rootDir: root });
    assert.equal(findings.length, 1);
    assert.match(findings[0].file, /public\/videos\/2024\/intro\.mp4$/);
  });
});

test('large-static-asset: returns [] when public/ does not exist', async () => {
  const root = await mkdtemp(join(tmpdir(), 'vo-no-public-'));
  try {
    const findings = await scan({ rootDir: root });
    assert.deepEqual(findings, []);
  } finally {
    await rm(root, { recursive: true, force: true });
  }
});

test('large-static-asset: metadata trafficIndependent=true (file size is fixed)', () => {
  // File size is fixed regardless of traffic — scanner-driven gate emits even
  // without route-mapping.
  assert.equal(metadata.trafficIndependent, true);
});