diff options
| author | John Pham <john.pham@vercel.com> | 2026-05-22 10:24:06 -0700 |
|---|---|---|
| committer | John Pham <john.pham@vercel.com> | 2026-05-22 10:24:06 -0700 |
| commit | cb122aa341137e86eb79e568eedd1c07115ba5c3 (patch) | |
| tree | aad9ca2bb86053fd9264360d85405ca6a7145277 /packages | |
| parent | da22a4c9e85b3a3f6f4b4e8cd415016117643bb2 (diff) | |
fix: harden vercel optimize project scope preflightjohnphamous/vercel-optimize-project-scope-preflight
Diffstat (limited to 'packages')
| -rw-r--r-- | packages/vercel-optimize-tests/test/collect-framework-preflight.test.mjs | 181 | ||||
| -rw-r--r-- | packages/vercel-optimize-tests/test/collection-command-docs.test.mjs | 8 |
2 files changed, 189 insertions, 0 deletions
diff --git a/packages/vercel-optimize-tests/test/collect-framework-preflight.test.mjs b/packages/vercel-optimize-tests/test/collect-framework-preflight.test.mjs index acf3e23..a34ca21 100644 --- a/packages/vercel-optimize-tests/test/collect-framework-preflight.test.mjs +++ b/packages/vercel-optimize-tests/test/collect-framework-preflight.test.mjs @@ -243,6 +243,85 @@ process.exit(66); } }); +test('collect-signals: stops on project/team mismatch before Observability Plus checks', async () => { + const scratch = await mkdtemp(join(tmpdir(), 'vo-project-scope-mismatch-')); + const bin = join(scratch, 'bin'); + try { + await mkdir(bin, { recursive: true }); + await mkdir(join(scratch, '.vercel'), { recursive: true }); + await writeFile(join(scratch, 'package.json'), JSON.stringify({ + dependencies: { next: '^15.3.0' }, + }), 'utf-8'); + await writeFile(join(scratch, '.vercel', 'project.json'), JSON.stringify({ + projectId: 'prj_target', + orgId: 'team_wrong', + }), 'utf-8'); + const fakeVercel = join(bin, 'vercel'); + await writeFile(fakeVercel, `#!/usr/bin/env node +const args = process.argv.slice(2); +function json(value, code = 0) { + process.stdout.write(JSON.stringify(value, null, 2) + '\\n'); + process.exit(code); +} +if (args[0] === '--version') { + process.stdout.write('54.1.0\\n'); + process.exit(0); +} +if (args[0] === 'whoami' && args[1] === '--format') { + json({ + username: 'test-user', + team: { id: 'team_other', slug: 'other-team', name: 'Other Team' }, + }); +} +if (args[0] === 'whoami') { + process.stdout.write('test-user\\n'); + process.exit(0); +} +if (args[0] === 'api') { + const path = args[1]; + if (path === '/v2/teams/team_wrong') { + json({ id: 'team_wrong', slug: 'wrong-team', billing: { plan: 'pro' } }); + } + if (path === '/v9/projects/prj_target?teamId=team_wrong') { + json({ error: { code: 'PROJECT_NOT_FOUND', message: 'Project not found' } }, 1); + } + if (path.startsWith('/v1/observability/manage/configuration/projects')) { + process.stderr.write('observability should not be checked before project ownership is verified\\n'); + process.exit(68); + } +} +if (args[0] === 'metrics' || args[0] === 'usage' || args[0] === 'contract') { + process.stderr.write('scoped collection should not run before project ownership is verified: ' + args.join(' ') + '\\n'); + process.exit(69); +} +process.stderr.write('unexpected vercel call: ' + args.join(' ') + '\\n'); +process.exit(66); +`, 'utf-8'); + await chmod(fakeVercel, 0o755); + + let err; + try { + await exec('node', [COLLECT], { + cwd: scratch, + env: { ...process.env, PATH: `${bin}:${process.env.PATH}` }, + maxBuffer: 8 * 1024 * 1024, + }); + } catch (e) { + err = e; + } + + assert.ok(err, 'collector should stop when the resolved team cannot read the project'); + assert.equal(err.code, 1); + assert.equal(err.stdout, ''); + assert.match(err.stderr, /PROJECT_SCOPE_MISMATCH/); + assert.doesNotMatch(err.stderr, /observability should not be checked/); + assert.doesNotMatch(err.stderr, /scoped collection should not run/); + assert.doesNotMatch(err.stderr, /Observability Plus is NOT usable/); + } finally { + await rm(scratch, { recursive: true, force: true }); + } +}); + test('collect-signals: user-owned projects use username scope without teamId query params', async () => { const scratch = await mkdtemp(join(tmpdir(), 'vo-user-scope-')); const bin = join(scratch, 'bin'); @@ -452,3 +531,105 @@ process.exit(66); await rm(scratch, { recursive: true, force: true }); } }); + +test('collect-signals: explicit projectId resolves matching owner from multi-project repo.json', async () => { + const scratch = await mkdtemp(join(tmpdir(), 'vo-explicit-repo-project-')); + const bin = join(scratch, 'bin'); + try { + await mkdir(bin, { recursive: true }); + await mkdir(join(scratch, '.vercel'), { recursive: true }); + await writeFile(join(scratch, 'package.json'), JSON.stringify({ + dependencies: { next: '^15.3.0' }, + }), 'utf-8'); + await writeFile(join(scratch, '.vercel', 'repo.json'), JSON.stringify({ + projects: [ + { id: 'prj_first', orgId: 'team_first' }, + { id: 'prj_second', orgId: 'team_second' }, + ], + }), 'utf-8'); + const fakeVercel = join(bin, 'vercel'); + await writeFile(fakeVercel, `#!/usr/bin/env node +const args = process.argv.slice(2); +function json(value, code = 0) { + process.stdout.write(JSON.stringify(value, null, 2) + '\\n'); + process.exit(code); +} +function requireScope(expected) { + const i = args.indexOf('--scope'); + if (i === -1 || args[i + 1] !== expected || args.includes('team_second')) { + process.stderr.write('missing expected scope ' + expected + ': ' + args.join(' ') + '\\n'); + process.exit(67); + } +} +if (args[0] === '--version') { + process.stdout.write('54.1.0\\n'); + process.exit(0); +} +if (args[0] === 'whoami' && args[1] === '--format') { + json({ + username: 'test-user', + team: { id: 'team_first', slug: 'first-team', name: 'First Team' }, + }); +} +if (args[0] === 'whoami') { + process.stdout.write('test-user\\n'); + process.exit(0); +} +if (args[0] === 'api') { + const path = args[1]; + if (path === '/v2/teams/team_second') { + json({ id: 'team_second', slug: 'second-team', billing: { plan: 'pro' } }); + } + if (path === '/v9/projects/prj_second?teamId=team_second') { + json({ id: 'prj_second', accountId: 'team_second', name: 'second-site' }); + } + if (path.startsWith('/v1/observability/manage/configuration/projects')) { + json({ error: { code: 'not_found', message: 'Observability Plus is not enabled' } }, 1); + } +} +if (args[0] === 'contract') { + requireScope('second-team'); + json({ context: 'second-team', commitments: [], totalCommitments: 0 }); +} +if (args[0] === 'usage') { + requireScope('second-team'); + json({ + context: 'second-team', + groupBy: { + dimension: 'project', + data: [{ + projectId: 'prj_second', + name: 'second-site', + services: [{ name: 'Function Invocations', billedCost: 2 }], + totals: { billedCost: 2 }, + }], + }, + services: [{ name: 'Function Invocations', billedCost: 2 }], + totals: { billedCost: 2 }, + }); +} +process.stderr.write('unexpected vercel call: ' + args.join(' ') + '\\n'); +process.exit(66); +`, 'utf-8'); + await chmod(fakeVercel, 0o755); + + const { stdout, stderr } = await exec('node', [COLLECT, 'prj_second', '--continue-without-observability'], { + cwd: scratch, + env: { ...process.env, PATH: `${bin}:${process.env.PATH}` }, + maxBuffer: 8 * 1024 * 1024, + }); + const out = JSON.parse(stdout); + assert.equal(out.projectId, 'prj_second'); + assert.equal(out.orgId, 'team_second'); + assert.equal(out.projectIdSource, 'arg+repo.json'); + assert.equal(out.commandScope.cliScope, 'second-team'); + assert.equal(out.commandScope.source, 'team-api'); + assert.equal(out.project.accountId, 'team_second'); + assert.equal(out.usageScope, 'project'); + assert.equal(out.usage.project.projectId, 'prj_second'); + assert.doesNotMatch(stderr, /unexpected vercel call/); + assert.doesNotMatch(stderr, /missing expected scope/); + } finally { + await rm(scratch, { recursive: true, force: true }); + } +}); diff --git a/packages/vercel-optimize-tests/test/collection-command-docs.test.mjs b/packages/vercel-optimize-tests/test/collection-command-docs.test.mjs index b891d70..a307df7 100644 --- a/packages/vercel-optimize-tests/test/collection-command-docs.test.mjs +++ b/packages/vercel-optimize-tests/test/collection-command-docs.test.mjs @@ -22,6 +22,14 @@ test('collection docs keep JSON stdout separate from status logs', async () => { assert.match(skill, /Do not combine streams/); }); +test('collection docs include an exact scope-confirmation prompt', async () => { + const skill = await readFile(join(ROOT, 'SKILL.md'), 'utf-8'); + assert.match(skill, /Use this prompt for `PROJECT_SCOPE_UNRESOLVED`, `SCOPE_UNRESOLVED`, or `PROJECT_SCOPE_MISMATCH`/); + assert.match(skill, /I can't safely identify the Vercel project and account for this audit yet\./); + assert.match(skill, /Please confirm the Vercel project name or ID and the team slug\/name/); + assert.match(skill, /before checking metrics/); +}); + test('Observability Plus scanner-only instructions preserve separate stderr logs', async () => { const reference = await readFile(join(ROOT, 'references', 'observability-plus.md'), 'utf-8'); assert.match( |
