diff options
| author | John Pham <john.pham@vercel.com> | 2026-05-22 09:57:46 -0700 |
|---|---|---|
| committer | John Pham <john.pham@vercel.com> | 2026-05-22 09:57:46 -0700 |
| commit | 72dd69e06bc51368d53ca7de2c8d75bc42fe75f7 (patch) | |
| tree | c87fdcee95d7935cacd0e1eecc2d8ea6a6e1ccdf /packages | |
| parent | c865495cd414c6105b56ae33a11ad3dc16a770cf (diff) | |
Harden vercel optimize project scopingjohnphamous/harden-vercel-optimize-scopingfeat/vercel-optimize-skill
Diffstat (limited to 'packages')
9 files changed, 490 insertions, 20 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 2188c03..d0365aa 100644 --- a/packages/vercel-optimize-tests/test/collect-framework-preflight.test.mjs +++ b/packages/vercel-optimize-tests/test/collect-framework-preflight.test.mjs @@ -34,6 +34,10 @@ if [ "$1" = "whoami" ]; then echo "test-user" exit 0 fi +if [ "$1" = "teams" ] && [ "$2" = "list" ]; then + echo '{"teams":[{"id":"team_test","slug":"test-team"}]}' + exit 0 +fi echo "unexpected vercel call: $*" >&2 exit 66 `, 'utf-8'); @@ -41,7 +45,7 @@ exit 66 const { stdout, stderr } = await exec('node', [COLLECT], { cwd: scratch, - env: { ...process.env, PATH: `${bin}:${process.env.PATH}` }, + env: { ...process.env, PATH: `${bin}:${process.env.PATH}`, VERCEL_PROJECT_ID: '', VERCEL_ORG_ID: '' }, maxBuffer: 8 * 1024 * 1024, }); const out = JSON.parse(stdout); @@ -56,3 +60,111 @@ exit 66 await rm(scratch, { recursive: true, force: true }); } }); + +test('collect-signals: ambiguous multi-project link stops before framework and metric calls', async () => { + const scratch = await mkdtemp(join(tmpdir(), 'vo-scope-preflight-')); + 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: '^16.1.6' }, + }), 'utf-8'); + await writeFile(join(scratch, '.vercel', 'repo.json'), JSON.stringify({ + projects: [ + { id: 'prj_web', orgId: 'team_test', name: 'web' }, + { id: 'prj_docs', orgId: 'team_test', name: 'docs' }, + ], + }), 'utf-8'); + const fakeVercel = join(bin, 'vercel'); + await writeFile(fakeVercel, `#!/bin/sh +if [ "$1" = "--version" ]; then + echo "53.0.0" + exit 0 +fi +if [ "$1" = "whoami" ]; then + echo "test-user" + exit 0 +fi +echo "unexpected vercel call: $*" >&2 +exit 66 +`, 'utf-8'); + await chmod(fakeVercel, 0o755); + + const { stdout, stderr } = await exec('node', [COLLECT], { + cwd: scratch, + env: { ...process.env, PATH: `${bin}:${process.env.PATH}`, VERCEL_PROJECT_ID: '', VERCEL_ORG_ID: '' }, + maxBuffer: 8 * 1024 * 1024, + }); + const out = JSON.parse(stdout); + assert.equal(out.scopeBlocker, 'ambiguous_project'); + assert.equal(out.usageError, 'NOT_COLLECTED_SCOPE_BLOCKED'); + assert.equal(out.scopeChoices.length, 2); + assert.match(stderr, /scope resolution blocked: ambiguous_project/); + assert.doesNotMatch(stderr, /unexpected vercel call/); + assert.doesNotMatch(stderr, /checking framework support/); + } finally { + await rm(scratch, { recursive: true, force: true }); + } +}); + +test('collect-signals: repo-linked project rootDirectory drives framework detection', async () => { + const scratch = await mkdtemp(join(tmpdir(), 'vo-repo-root-framework-')); + const bin = join(scratch, 'bin'); + try { + await mkdir(bin, { recursive: true }); + await mkdir(join(scratch, '.vercel'), { recursive: true }); + await mkdir(join(scratch, 'apps', 'web'), { recursive: true }); + await writeFile(join(scratch, 'package.json'), JSON.stringify({ + private: true, + workspaces: ['apps/*'], + }), 'utf-8'); + await writeFile(join(scratch, 'apps', 'web', 'package.json'), JSON.stringify({ + dependencies: { next: '^16.1.6' }, + }), 'utf-8'); + await writeFile(join(scratch, '.vercel', 'repo.json'), JSON.stringify({ + orgId: 'team_test', + projects: { + 'apps/web': 'prj_web', + 'apps/docs': 'prj_docs', + }, + }), 'utf-8'); + const fakeVercel = join(bin, 'vercel'); + await writeFile(fakeVercel, `#!/bin/sh +if [ "$1" = "--version" ]; then + echo "53.0.0" + exit 0 +fi +if [ "$1" = "whoami" ]; then + echo "test-user" + exit 0 +fi +if [ "$1" = "teams" ] && [ "$2" = "list" ]; then + echo '{"teams":[{"id":"team_test","slug":"test-team"}]}' + exit 0 +fi +if [ "$1" = "api" ]; then + echo '{"error":{"code":"not_found","message":"Observability Plus is not enabled"}}' + exit 1 +fi +echo "unexpected vercel call: $*" >&2 +exit 66 +`, 'utf-8'); + await chmod(fakeVercel, 0o755); + + const { stdout, stderr } = await exec('node', [COLLECT, 'prj_web'], { + cwd: scratch, + env: { ...process.env, PATH: `${bin}:${process.env.PATH}`, VERCEL_PROJECT_ID: '', VERCEL_ORG_ID: '' }, + maxBuffer: 8 * 1024 * 1024, + }); + const out = JSON.parse(stdout); + assert.equal(out.scopeBlocker, null); + assert.equal(out.stack.framework, 'next'); + assert.equal(out.stack.rootDirectory, 'apps/web'); + assert.equal(out.observabilityPlusBlocker, 'oplus_not_enabled'); + assert.match(stderr, /framework=next@16\.1\.6 support=supported/); + assert.doesNotMatch(stderr, /unexpected vercel call/); + } 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..a366134 100644 --- a/packages/vercel-optimize-tests/test/collection-command-docs.test.mjs +++ b/packages/vercel-optimize-tests/test/collection-command-docs.test.mjs @@ -19,6 +19,9 @@ test('collection docs keep JSON stdout separate from status logs', async () => { } assert.match(skill, /JSON\.parse\(require\("fs"\)\.readFileSync\(process\.argv\[1\], "utf8"\)\)/); + assert.match(skill, /jq '\{scopeBlocker, scopeBlockerDetail, scopeChoices/); + assert.match(skill, /before scanning source/); + assert.match(skill, /Do not run `scan-codebase\.mjs`, `merge-signals\.mjs`, gating, or scanner-only mode from an unresolved project\/team/); assert.match(skill, /Do not combine streams/); }); diff --git a/packages/vercel-optimize-tests/test/observability-preflight.test.mjs b/packages/vercel-optimize-tests/test/observability-preflight.test.mjs index 1a05373..d642029 100644 --- a/packages/vercel-optimize-tests/test/observability-preflight.test.mjs +++ b/packages/vercel-optimize-tests/test/observability-preflight.test.mjs @@ -33,7 +33,7 @@ test('classifyObservabilityPlusConfiguration: project_disabled when this project }); }); -test('classifyObservabilityPlusConfiguration: no_oplus_probe on public API not-enabled response', () => { +test('classifyObservabilityPlusConfiguration: oplus_not_enabled on public API not-enabled response', () => { const r = classifyObservabilityPlusConfiguration({ ok: false, code: 'not_found', @@ -42,12 +42,13 @@ test('classifyObservabilityPlusConfiguration: no_oplus_probe on public API not-e assert.equal(r.ok, true); assert.equal(r.access, false); - assert.equal(r.blocker, 'no_oplus_probe'); - assert.match(r.detail, /^Route-level metrics are unavailable/); + assert.equal(r.blocker, 'oplus_not_enabled'); + assert.match(r.detail, /^The metrics API reported/); assert.match(r.detail, /not enabled/); + assert.match(r.detail, /current Vercel scope/); }); -test('classifyObservabilityPlusConfiguration: no_oplus_probe on CLI OPLUS_REQUIRED response', () => { +test('classifyObservabilityPlusConfiguration: oplus_not_enabled on CLI OPLUS_REQUIRED response', () => { const r = classifyObservabilityPlusConfiguration({ ok: false, code: 'OPLUS_REQUIRED', @@ -56,7 +57,57 @@ test('classifyObservabilityPlusConfiguration: no_oplus_probe on CLI OPLUS_REQUIR assert.equal(r.ok, true); assert.equal(r.access, false); - assert.equal(r.blocker, 'no_oplus_probe'); + assert.equal(r.blocker, 'oplus_not_enabled'); +}); + +test('classifyObservabilityPlusConfiguration: project_disabled on project-level not-enabled response', () => { + const r = classifyObservabilityPlusConfiguration({ + ok: false, + code: 'OPLUS_REQUIRED', + stderr: 'Observability Plus is not enabled for this project', + }, { projectId: 'prj_target' }); + + assert.equal(r.ok, true); + assert.equal(r.access, false); + assert.equal(r.blocker, 'project_disabled'); + assert.match(r.detail, /not enabled for this project/); +}); + +test('classifyObservabilityPlusConfiguration: project_disabled on project disabled wording', () => { + const r = classifyObservabilityPlusConfiguration({ + ok: false, + code: 'OPLUS_REQUIRED', + stderr: 'Observability Plus is disabled for this project', + }, { projectId: 'prj_target' }); + + assert.equal(r.ok, true); + assert.equal(r.access, false); + assert.equal(r.blocker, 'project_disabled'); +}); + +test('classifyObservabilityPlusConfiguration: project_disabled on does-not-have-enabled wording', () => { + const r = classifyObservabilityPlusConfiguration({ + ok: false, + code: 'OPLUS_REQUIRED', + stderr: 'This project does not have Observability Plus enabled', + }, { projectId: 'prj_target' }); + + assert.equal(r.ok, true); + assert.equal(r.access, false); + assert.equal(r.blocker, 'project_disabled'); +}); + +test('classifyObservabilityPlusConfiguration: generic Observability Plus failure is inconclusive', () => { + const r = classifyObservabilityPlusConfiguration({ + ok: false, + code: 'EXIT_1', + stderr: 'Failed to query Observability Plus configuration endpoint.', + }, { projectId: 'prj_target' }); + + assert.equal(r.ok, false); + assert.equal(r.access, null); + assert.equal(r.blocker, 'unknown'); + assert.doesNotMatch(r.detail, /not enabled/i); }); test('classifyObservabilityPlusConfiguration: generic 404 not-enabled text is inconclusive', () => { diff --git a/packages/vercel-optimize-tests/test/oplus-diagnose.test.mjs b/packages/vercel-optimize-tests/test/oplus-diagnose.test.mjs index 94abf62..c9f9737 100644 --- a/packages/vercel-optimize-tests/test/oplus-diagnose.test.mjs +++ b/packages/vercel-optimize-tests/test/oplus-diagnose.test.mjs @@ -6,10 +6,58 @@ import { test } from 'node:test'; import assert from 'node:assert/strict'; import { diagnoseObservabilityPlus } from '../../../skills/vercel-optimize/scripts/collect-signals.mjs'; -test('oplus diag: probe failed → no_oplus_probe', () => { +test('oplus diag: probe failed → oplus_probe_failed without disabled claim', () => { const r = diagnoseObservabilityPlus({}, false); assert.equal(r.usable, false); - assert.equal(r.blocker, 'no_oplus_probe'); + assert.equal(r.blocker, 'oplus_probe_failed'); + assert.doesNotMatch(r.detail, /does not have Observability Plus enabled|not enabled/i); +}); + +test('oplus diag: schema probe generic failure → oplus_probe_failed without disabled claim', () => { + const r = diagnoseObservabilityPlus({}, { + ok: false, + access: null, + code: 'EXIT_1', + stderr: 'Failed to query Observability Plus configuration endpoint.', + }); + assert.equal(r.usable, false); + assert.equal(r.blocker, 'oplus_probe_failed'); + assert.match(r.detail, /code=EXIT_1/); + assert.doesNotMatch(r.detail, /not enabled/i); +}); + +test('oplus diag: generated inconclusive detail is not reclassified as disabled', () => { + const r = diagnoseObservabilityPlus({}, { + ok: false, + access: null, + code: 'EXIT_1', + detail: 'This does not prove Observability Plus is disabled.', + }); + assert.equal(r.usable, false); + assert.equal(r.blocker, 'oplus_probe_failed'); +}); + +test('oplus diag: schema probe project-level not-enabled response → project_disabled', () => { + const r = diagnoseObservabilityPlus({}, { + ok: false, + access: false, + code: 'OPLUS_REQUIRED', + stderr: 'Observability Plus is not enabled for this project.', + }); + assert.equal(r.usable, false); + assert.equal(r.blocker, 'project_disabled'); + assert.match(r.detail, /not enabled for this project/); +}); + +test('oplus diag: schema probe project disabled wording → project_disabled', () => { + const r = diagnoseObservabilityPlus({}, { + ok: false, + access: false, + code: 'OPLUS_REQUIRED', + stderr: 'This project does not have Observability Plus enabled.', + }); + assert.equal(r.usable, false); + assert.equal(r.blocker, 'project_disabled'); }); test('oplus diag: queries all returned payment_required → payment_required blocker', () => { @@ -27,7 +75,7 @@ test('oplus diag: queries all returned payment_required → payment_required blo assert.match(r.detail, /4\/4/); }); -test('oplus diag: payment_required with subscription-required text → no_oplus_probe', () => { +test('oplus diag: payment_required with subscription-required text → oplus_not_enabled', () => { const metrics = { observabilityPlusCanary: { ok: false, @@ -37,8 +85,22 @@ test('oplus diag: payment_required with subscription-required text → no_oplus_ }; const r = diagnoseObservabilityPlus(metrics, true); assert.equal(r.usable, false); - assert.equal(r.blocker, 'no_oplus_probe'); - assert.match(r.detail, /route-level Observability Plus data/); + assert.equal(r.blocker, 'oplus_not_enabled'); + assert.match(r.detail, /Observability Plus is not enabled/); +}); + +test('oplus diag: payment_required with project-not-enabled text → project_disabled', () => { + const metrics = { + observabilityPlusCanary: { + ok: false, + code: 'payment_required', + message: 'Observability Plus is not enabled for this project', + }, + }; + const r = diagnoseObservabilityPlus(metrics, true); + assert.equal(r.usable, false); + assert.equal(r.blocker, 'project_disabled'); + assert.match(r.detail, /not enabled for this project/); }); test('oplus diag: queries all hit daily quota → daily_quota_exceeded blocker', () => { @@ -128,10 +190,11 @@ test('oplus diag: mixed success + daily quota → still usable when any query re assert.equal(r.blocker, null); }); -test('oplus diag: probe OK but no metrics attempted → no_oplus_probe (defensive)', () => { +test('oplus diag: probe OK but no metrics attempted → oplus_probe_failed (defensive)', () => { const r = diagnoseObservabilityPlus({}, true); assert.equal(r.usable, false); - assert.equal(r.blocker, 'no_oplus_probe'); + assert.equal(r.blocker, 'oplus_probe_failed'); + assert.doesNotMatch(r.detail, /does not have Observability Plus enabled|not enabled/i); }); test('oplus diag: unknown failure code falls back to all_failed_other', () => { diff --git a/packages/vercel-optimize-tests/test/public-release-safety.test.mjs b/packages/vercel-optimize-tests/test/public-release-safety.test.mjs index f263910..83e0035 100644 --- a/packages/vercel-optimize-tests/test/public-release-safety.test.mjs +++ b/packages/vercel-optimize-tests/test/public-release-safety.test.mjs @@ -55,6 +55,7 @@ test('public release safety: public docs do not imply project ID replaces linkag const text = await readFile(file, 'utf8'); assert.doesNotMatch(text, /Linked Vercel project(?:,| directory) or `VERCEL_PROJECT_ID`/, file); assert.doesNotMatch(text, /Linked Vercel project, or `VERCEL_PROJECT_ID`/, file); + assert.match(text, /vercel link --repo|repo root from `vercel link --repo`/, file); } }); diff --git a/packages/vercel-optimize-tests/test/render-report.test.mjs b/packages/vercel-optimize-tests/test/render-report.test.mjs index aa3c116..ec7da20 100644 --- a/packages/vercel-optimize-tests/test/render-report.test.mjs +++ b/packages/vercel-optimize-tests/test/render-report.test.mjs @@ -270,12 +270,65 @@ test('renderReport: handles missing observabilityPlus gracefully', () => { const md = renderReport({ recommendations: [], gated: [], - signals: { ...baseSignals, observabilityPlus: false }, + signals: { + ...baseSignals, + observabilityPlus: false, + observabilityPlusBlocker: 'oplus_not_enabled', + observabilityPlusBlockerDetail: 'The metrics API reported that Observability Plus is not enabled for the current Vercel scope.', + }, }); - assert.match(md, /Not enabled — analysis based on billing \+ scanner findings/); + assert.match(md, /Observability Plus not enabled — analysis based on billing \+ scanner findings/); assert.match(md, /Observability Plus not enabled/); }); +test('renderReport: inconclusive Observability probe does not claim Observability Plus is disabled', () => { + const md = renderReport({ + recommendations: [], + gated: [], + signals: { + ...baseSignals, + observabilityPlus: false, + observabilityPlusUsable: false, + observabilityPlusBlocker: 'oplus_probe_failed', + observabilityPlusBlockerDetail: '`vercel metrics schema` returned non-OK. This does not prove Observability Plus is disabled.', + }, + }); + assert.match(md, /Per-route metrics probe failed/); + assert.doesNotMatch(md, /Not enabled — analysis based/); + assert.doesNotMatch(md, /Observability Plus not enabled/); +}); + +test('renderReport: missing Observability fields do not imply metrics were included', () => { + const { observabilityPlus, observabilityPlusUsable, observabilityPlusBlocker, ...signals } = baseSignals; + const md = renderReport({ + recommendations: [], + gated: [], + signals, + }); + + assert.match(md, /Per-route metrics unavailable — analysis based on billing \+ scanner findings/); + assert.match(md, /Per-route metrics unavailable — route latency \/ cache-hit \/ cold-start analysis was skipped/); + assert.doesNotMatch(md, /Observability Plus enabled — per-route metrics included/); +}); + +test('renderReport: unresolved project scope does not claim metrics were included', () => { + const md = renderReport({ + recommendations: [], + gated: [], + signals: { + ...baseSignals, + scopeBlocker: 'ambiguous_project', + scopeBlockerDetail: 'This directory is linked to multiple Vercel projects.', + observabilityPlus: null, + observabilityPlusUsable: null, + observabilityPlusBlocker: null, + }, + }); + assert.match(md, /Project\/team scope unresolved — analysis cannot use Vercel metrics/); + assert.match(md, /Project\/team scope unresolved — Vercel usage and route metrics were not collected/); + assert.doesNotMatch(md, /Observability Plus enabled — per-route metrics included/); +}); + test('renderReport: hides sanitizerTrail by default and does not emit generic review notes', () => { const rec = { ...sampleRec, diff --git a/packages/vercel-optimize-tests/test/scope-resolution.test.mjs b/packages/vercel-optimize-tests/test/scope-resolution.test.mjs new file mode 100644 index 0000000..d585b91 --- /dev/null +++ b/packages/vercel-optimize-tests/test/scope-resolution.test.mjs @@ -0,0 +1,178 @@ +import { test } from 'node:test'; +import assert from 'node:assert/strict'; +import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises'; +import { tmpdir } from 'node:os'; +import { join } from 'node:path'; +import { + readProjectJson, + resolveProjectId, +} from '../../../skills/vercel-optimize/lib/vercel.mjs'; + +async function withScratch(fn) { + const scratch = await mkdtemp(join(tmpdir(), 'vo-scope-')); + try { + await mkdir(join(scratch, '.vercel'), { recursive: true }); + return await fn(scratch); + } finally { + await rm(scratch, { recursive: true, force: true }); + } +} + +function withEnv(env, fn) { + const prev = {}; + for (const key of Object.keys(env)) { + prev[key] = process.env[key]; + if (env[key] == null) delete process.env[key]; + else process.env[key] = env[key]; + } + return Promise.resolve() + .then(fn) + .finally(() => { + for (const key of Object.keys(env)) { + if (prev[key] == null) delete process.env[key]; + else process.env[key] = prev[key]; + } + }); +} + +async function writeRepoJson(cwd, projects, extra = {}) { + await writeFile(join(cwd, '.vercel', 'repo.json'), JSON.stringify({ ...extra, projects }), 'utf-8'); +} + +async function writeProjectJson(cwd, project) { + await writeFile(join(cwd, '.vercel', 'project.json'), JSON.stringify(project), 'utf-8'); +} + +test('resolveProjectId: blocks multi-project repo.json without an explicit project', async () => withScratch(async (cwd) => { + await writeRepoJson(cwd, [ + { id: 'prj_web', orgId: 'team_a', name: 'web' }, + { id: 'prj_docs', orgId: 'team_a', name: 'docs' }, + ]); + + const r = await withEnv({ VERCEL_PROJECT_ID: null, VERCEL_ORG_ID: null }, () => resolveProjectId(null, cwd)); + + assert.equal(r.ok, false); + assert.equal(r.blocker, 'ambiguous_project'); + assert.equal(r.choices.length, 2); +})); + +test('resolveProjectId: explicit project selects the matching repo.json project and team', async () => withScratch(async (cwd) => { + await writeRepoJson(cwd, [ + { id: 'prj_web', orgId: 'team_a', name: 'web' }, + { id: 'prj_docs', orgId: 'team_b', name: 'docs', rootDirectory: 'apps/docs' }, + ]); + + const r = await withEnv({ VERCEL_PROJECT_ID: null, VERCEL_ORG_ID: null }, () => resolveProjectId('prj_docs', cwd)); + + assert.equal(r.ok, true); + assert.equal(r.projectId, 'prj_docs'); + assert.equal(r.orgId, 'team_b'); + assert.equal(r.rootDirectory, 'apps/docs'); +})); + +test('resolveProjectId: repo.json project map inherits top-level team scope', async () => withScratch(async (cwd) => { + await writeRepoJson(cwd, { + 'apps/web': 'prj_web', + 'apps/docs': { id: 'prj_docs', name: 'docs' }, + }, { orgId: 'team_a', teamSlug: 'acme' }); + + const r = await withEnv({ VERCEL_PROJECT_ID: null, VERCEL_ORG_ID: null }, () => resolveProjectId('prj_docs', cwd)); + + assert.equal(r.ok, true); + assert.equal(r.projectId, 'prj_docs'); + assert.equal(r.orgId, 'team_a'); + assert.equal(r.orgSlug, 'acme'); + assert.equal(r.rootDirectory, 'apps/docs'); +})); + +test('resolveProjectId: repo.json project array inherits top-level team scope', async () => withScratch(async (cwd) => { + await writeRepoJson(cwd, [ + { id: 'prj_docs', name: 'docs', rootDirectory: 'apps/docs' }, + ], { orgId: 'team_a', teamSlug: 'acme' }); + + const r = await withEnv({ VERCEL_PROJECT_ID: null, VERCEL_ORG_ID: null }, () => resolveProjectId(null, cwd)); + + assert.equal(r.ok, true); + assert.equal(r.projectId, 'prj_docs'); + assert.equal(r.orgId, 'team_a'); + assert.equal(r.orgSlug, 'acme'); +})); + +test('resolveProjectId: generic repo.json slug is not treated as a team slug', async () => withScratch(async (cwd) => { + await writeRepoJson(cwd, [ + { id: 'prj_docs', orgId: 'team_b', slug: 'docs-project-slug' }, + ]); + + const r = await withEnv({ VERCEL_PROJECT_ID: null, VERCEL_ORG_ID: null }, () => resolveProjectId(null, cwd)); + + assert.equal(r.ok, true); + assert.equal(r.orgId, 'team_b'); + assert.equal(r.orgSlug, null); +})); + +test('resolveProjectId: explicit project mismatch blocks instead of mixing API and metrics scope', async () => withScratch(async (cwd) => { + await writeProjectJson(cwd, { projectId: 'prj_linked', orgId: 'team_a' }); + + const r = await withEnv({ VERCEL_PROJECT_ID: null, VERCEL_ORG_ID: null }, () => resolveProjectId('prj_requested', cwd)); + + assert.equal(r.ok, false); + assert.equal(r.blocker, 'project_link_mismatch'); + assert.equal(r.choices[0].projectId, 'prj_linked'); +})); + +test('resolveProjectId: env project ID must match the linked project', async () => withScratch(async (cwd) => { + await writeProjectJson(cwd, { projectId: 'prj_linked', orgId: 'team_a' }); + + const r = await withEnv({ VERCEL_PROJECT_ID: 'prj_other', VERCEL_ORG_ID: null }, () => resolveProjectId(null, cwd)); + + assert.equal(r.ok, false); + assert.equal(r.blocker, 'project_link_mismatch'); +})); + +test('resolveProjectId: VERCEL_ORG_ID conflict with linked team blocks collection', async () => withScratch(async (cwd) => { + await writeProjectJson(cwd, { projectId: 'prj_linked', orgId: 'team_a' }); + + const r = await withEnv({ VERCEL_PROJECT_ID: null, VERCEL_ORG_ID: 'team_b' }, () => resolveProjectId(null, cwd)); + + assert.equal(r.ok, false); + assert.equal(r.blocker, 'team_scope_conflict'); +})); + +test('resolveProjectId: missing linked team blocks even when VERCEL_ORG_ID is set', async () => withScratch(async (cwd) => { + await writeProjectJson(cwd, { projectId: 'prj_linked' }); + + const r = await withEnv({ VERCEL_PROJECT_ID: null, VERCEL_ORG_ID: 'team_from_env' }, () => resolveProjectId(null, cwd)); + + assert.equal(r.ok, false); + assert.equal(r.blocker, 'team_scope_missing'); + assert.match(r.detail, /cannot verify/); +})); + +test('resolveProjectId: unlinked cwd blocks even when project ID was supplied', async () => withScratch(async (cwd) => { + const r = await withEnv({ VERCEL_PROJECT_ID: null, VERCEL_ORG_ID: null }, () => resolveProjectId('prj_requested', cwd)); + + assert.equal(r.ok, false); + assert.equal(r.blocker, 'not_linked'); + assert.match(r.detail, /project ID alone is not enough/); +})); + +test('readProjectJson: multi-project repo returns exact match when projectId is supplied', async () => withScratch(async (cwd) => { + await writeRepoJson(cwd, [ + { id: 'prj_web', orgId: 'team_a', name: 'web' }, + { id: 'prj_docs', orgId: 'team_b', name: 'docs' }, + ]); + + const r = await readProjectJson(cwd, { projectId: 'prj_docs' }); + + assert.equal(r.projectId, 'prj_docs'); + assert.equal(r.orgId, 'team_b'); +})); + +test('readProjectJson: multi-project repo without projectId is intentionally ambiguous', async () => withScratch(async (cwd) => { + await writeRepoJson(cwd, [ + { id: 'prj_web', orgId: 'team_a', name: 'web' }, + { id: 'prj_docs', orgId: 'team_b', name: 'docs' }, + ]); + + assert.equal(await readProjectJson(cwd), null); +})); diff --git a/packages/vercel-optimize-tests/test/time-window.test.mjs b/packages/vercel-optimize-tests/test/time-window.test.mjs index dcfb2f9..046dabb 100644 --- a/packages/vercel-optimize-tests/test/time-window.test.mjs +++ b/packages/vercel-optimize-tests/test/time-window.test.mjs @@ -34,3 +34,12 @@ test('collect-signals.mjs: passes TIME_WINDOW (not a string literal) to queryMet assert.equal(literalSince, null, `collect-signals.mjs must not hard-pin since to a literal; found: ${literalSince}`); }); + +test('metric query call sites pass explicit projectId to avoid currentTeam drift', async () => { + const collectSrc = await readFile(join(HERE, '..', '..', '..', 'skills', 'vercel-optimize', 'scripts', 'collect-signals.mjs'), 'utf-8'); + const deepDiveSrc = await readFile(join(HERE, '..', '..', '..', 'skills', 'vercel-optimize', 'scripts', 'deep-dive.mjs'), 'utf-8'); + assert.match(collectSrc, /queryMetric\('vercel\.request\.count'[\s\S]*projectId:\s*project\.projectId/); + assert.match(collectSrc, /queryMetric\(entry\.metricId[\s\S]*projectId,\s*\n/); + assert.match(deepDiveSrc, /const scope = merged\.scopeResolution\?\.cliScope \|\| merged\.orgId \|\| undefined/); + assert.match(deepDiveSrc, /queryMetric\(spec\.metricId[\s\S]*projectId:\s*merged\.projectId/); +}); diff --git a/packages/vercel-optimize-tests/test/voice-drift.test.mjs b/packages/vercel-optimize-tests/test/voice-drift.test.mjs index 750de6c..6e87942 100644 --- a/packages/vercel-optimize-tests/test/voice-drift.test.mjs +++ b/packages/vercel-optimize-tests/test/voice-drift.test.mjs @@ -41,22 +41,22 @@ test('voice drift: no markdown file uses Observability Plus shorthand', async () test('voice drift: Observability Plus blocker question uses exact plain copy', async () => { const content = await readFile(join(ROOT, 'references', 'observability-plus.md'), 'utf-8'); - assert.match(content, /"header": "Observability Plus"/); + assert.match(content, /"header": "Metrics access"/); assert.match( content, - /"question": "Enable Observability Plus and re-run, or continue with a limited scanner-only audit\?"/ + /"question": "Fix per-route metrics access and re-run, or continue with a limited scanner-only audit\?"/ ); - assert.match(content, /"label": "Enable and re-run"/); + assert.match(content, /"label": "Fix and re-run"/); assert.match(content, /"label": "Run scanner-only"/); assert.doesNotMatch(content, /\b(O11y|OPlus|Oplus|O-Plus|perf|CWV)\b|obs\+/); }); test('voice drift: Observability Plus post-choice copy avoids IDs and pricing language', async () => { const content = await readFile(join(ROOT, 'references', 'observability-plus.md'), 'utf-8'); - assert.match(content, /If the user chooses \*\*Enable and re-run\*\*/); + assert.match(content, /If the user chooses \*\*Fix and re-run\*\*/); assert.match( content, - /Enable Observability Plus from the Vercel dashboard's Observability tab, then tell me to rerun\./ + /Fix per-route metrics access, then tell me to rerun\./ ); assert.match(content, /Do not include raw team IDs, org IDs, project IDs, pricing language/); assert.doesNotMatch(content, /paid add-on|paid feature|just say the word/i); |
