summaryrefslogtreecommitdiff
path: root/packages/vercel-optimize-tests/test/observations.test.mjs
blob: dba58b3ff41d8521f68fb23901b9aa1954003473 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Sub-agent observations: abstentions carry an optional `observation` field
// for findings that aren't perf recs but matter (deploy regression, error storm).

import { test } from 'node:test';
import assert from 'node:assert/strict';
import { renderReport } from '../../../skills/vercel-optimize/lib/render-report.mjs';
import { buildBrief, citationSubset } from '../../../skills/vercel-optimize/lib/investigation-brief.mjs';

const baseSignals = {
  stack: { framework: 'next', frameworkVersion: '16.3.0', hasAppRouter: true, orm: 'none' },
  plan: { plan: 'pro' },
  observabilityPlus: true,
};

test('investigation brief: documents the observation schema (B1)', async () => {
  const candidate = {
    kind: 'slow_route',
    route: '/event',
    scope: 'route',
    priority: 100,
    confidence: 0.9,
    o11ySignal: 'inv=2.8M,p95=3010ms',
    question: 'What is the bottleneck in /event?',
    evidence: { deepDive: { latency: { p95: 3010 } } },
  };
  const citations = await citationSubset('slow_route', 'next', '16.3.0');
  const md = buildBrief({
    candidate,
    candidateIndex: 0,
    candidateGroup: 'toLaunch',
    files: ['app/event/page.tsx'],
    signals: baseSignals,
    citations,
    playbookId: null,
    playbookBody: null,
    frameworkPlaybookId: null,
    frameworkPlaybookBody: null,
    generatedAt: null,
  });
  assert.match(md, /Abstain with an observation/);
  assert.match(md, /"observation":/);
  assert.match(md, /"summary":/);
  assert.match(md, /"evidence":/);
  assert.match(md, /"suggestedAction":/);
  assert.match(md, /"kind":/);
});

test('renderReport: emits Observations from investigation section when present', () => {
  const observations = [
    {
      candidateRef: 'slow_route:/event',
      summary: 'perDeployment p95 spiked from 256ms to 3135ms across the last 3 deployments — likely regression.',
      evidence: 'deployment-slow p95=3135ms vs deployment-fast p95=256ms',
      suggestedAction: 'Check the changeset between those deployments for added awaits or new external API calls.',
      kind: 'regression',
    },
  ];
  const md = renderReport({
    recommendations: [],
    gated: [],
    abstentions: [],
    observations,
    signals: baseSignals,
    opts: { projectName: 'x' },
  });
  assert.match(md, /## Observations from investigation/);
  assert.match(md, /per-deployment 95th percentile spiked/);
  assert.match(md, /regression/);
  assert.match(md, /deployment-slow/);
});

test('renderReport: no Observations section when none provided', () => {
  const md = renderReport({
    recommendations: [],
    gated: [],
    abstentions: [],
    observations: [],
    signals: baseSignals,
    opts: { projectName: 'x' },
  });
  assert.ok(!md.includes('## Observations from investigation'));
});

test('summarizeDeepDiveFailures: emits notice when >=50% of entries failed', async () => {
  const { summarizeDeepDiveFailures } = await import('../../../skills/vercel-optimize/lib/investigation-brief.mjs');
  const out = summarizeDeepDiveFailures({
    latencyP50: { value: 100 },
    latencyP95: { value: 500 },
    startTypeSplit: { ok: false, code: 'RATE_LIMITED' },
    cacheBreakdown: { ok: false, code: 'RATE_LIMITED' },
    perDeployment: { ok: false, code: 'PAYMENT_REQUIRED' },
  });
  assert.match(out, /3 of 5 deep-dive signals failed/);
  assert.match(out, /RATE_LIMITED|PAYMENT_REQUIRED/);
});

test('summarizeDeepDiveFailures: returns null when fewer than 3 entries failed and <50%', async () => {
  const { summarizeDeepDiveFailures } = await import('../../../skills/vercel-optimize/lib/investigation-brief.mjs');
  const out = summarizeDeepDiveFailures({
    a: { value: 1 }, b: { value: 2 }, c: { value: 3 }, d: { value: 4 },
    e: { ok: false, code: 'RATE_LIMITED' },
    f: { ok: false, code: 'RATE_LIMITED' },
  });
  assert.equal(out, null);
});

test('summarizeDeepDiveFailures: returns null on empty deep-dive', async () => {
  const { summarizeDeepDiveFailures } = await import('../../../skills/vercel-optimize/lib/investigation-brief.mjs');
  assert.equal(summarizeDeepDiveFailures({}), null);
  assert.equal(summarizeDeepDiveFailures(null), null);
});

test('renderReport: observation table escapes pipes in evidence cell', () => {
  const observations = [{
    candidateRef: 'slow_route:/x',
    summary: 'pipe | in | summary',
    evidence: 'value | with | pipes',
    suggestedAction: 'do | thing',
    kind: 'other',
  }];
  const md = renderReport({ recommendations: [], gated: [], observations, signals: baseSignals });
  const obsTableRow = md.split('\n').find((l) => l.startsWith('|') && l.includes('pipe \\| in \\| summary'));
  assert.ok(obsTableRow, `expected escaped pipes in observation row, got md:\n${md}`);
});