summaryrefslogtreecommitdiff
path: root/packages/vercel-optimize-tests/test/vercel-command.test.mjs
blob: c7bc2800b89e0ca6f0973eb2d201199388e85fb1 (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
100
101
102
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { win32 } from 'node:path';
import { resolveVercelCommand } from '../../../skills/vercel-optimize/lib/vercel.mjs';

const NODE = 'C:\\Program Files\\nodejs\\node.exe';

function fakeFs({ files = [], reads = {} } = {}) {
  const fileSet = new Set(files.map((file) => win32.normalize(file)));
  const readMap = new Map(
    Object.entries(reads).map(([file, text]) => [win32.normalize(file), text])
  );
  return {
    exists: (file) => fileSet.has(win32.normalize(file)),
    readText: (file) => {
      const key = win32.normalize(file);
      if (!readMap.has(key)) throw new Error(`missing fixture: ${file}`);
      return readMap.get(key);
    },
  };
}

test('resolveVercelCommand: POSIX keeps bare vercel command', () => {
  assert.deepEqual(
    resolveVercelCommand({
      platform: 'linux',
      env: { PATH: '/repo/node_modules/.bin:/usr/local/bin' },
      execPath: '/usr/local/bin/node',
      exists: () => false,
    }),
    { file: 'vercel', prefix: [] }
  );
});

test('resolveVercelCommand: Windows resolves npm global package entry', () => {
  const bin = 'C:\\Users\\me\\AppData\\Roaming\\npm';
  const entry = win32.join(bin, 'node_modules', 'vercel', 'dist', 'vc.js');
  const fs = fakeFs({ files: [entry] });

  assert.deepEqual(
    resolveVercelCommand({
      platform: 'win32',
      env: { PATH: bin },
      execPath: NODE,
      ...fs,
    }),
    { file: NODE, prefix: [entry] }
  );
});

test('resolveVercelCommand: Windows resolves local node_modules .bin package entry', () => {
  const bin = 'C:\\repo\\node_modules\\.bin';
  const entry = 'C:\\repo\\node_modules\\vercel\\dist\\vc.js';
  const fs = fakeFs({ files: [entry] });

  assert.deepEqual(
    resolveVercelCommand({
      platform: 'win32',
      env: { PATH: bin },
      execPath: NODE,
      ...fs,
    }),
    { file: NODE, prefix: [entry] }
  );
});

test('resolveVercelCommand: Windows parses vercel.cmd shim targets with spaces', () => {
  const bin = 'C:\\Users\\me\\App Data\\pnpm';
  const shim = win32.join(bin, 'vercel.cmd');
  const entry = win32.normalize(win32.join(bin, '..', 'store', 'vercel', 'dist', 'vc.js'));
  const fs = fakeFs({
    files: [shim, entry],
    reads: {
      [shim]: '@ECHO off\r\n"%_prog%" "%~dp0\\..\\store\\vercel\\dist\\vc.js" %*\r\n',
    },
  });

  assert.deepEqual(
    resolveVercelCommand({
      platform: 'win32',
      env: { Path: bin },
      execPath: NODE,
      ...fs,
    }),
    { file: NODE, prefix: [entry] }
  );
});

test('resolveVercelCommand: Windows reports missing instead of falling back to .cmd', () => {
  const bin = 'C:\\repo\\node_modules\\.bin';
  const fs = fakeFs();

  assert.deepEqual(
    resolveVercelCommand({
      platform: 'win32',
      env: { PATH: bin },
      execPath: NODE,
      ...fs,
    }),
    { file: NODE, prefix: [], missing: true }
  );
});