summaryrefslogtreecommitdiff
path: root/packages/vercel-optimize-tests/test/support-topics.test.mjs
blob: 97d253d05c6f5e25267cf03eeb789900fdd9bbb8 (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import { test } from 'node:test';
import assert from 'node:assert/strict';
import {
  loadSupportTopics,
  parseSupportTopic,
  renderSupportTopics,
  supportTopicSubset,
  validateSupportTopics,
} from '../../../skills/vercel-optimize/lib/support-topics.mjs';

const next15Signals = {
  stack: {
    framework: 'next',
    frameworkVersion: '15.4.10',
    hasAppRouter: true,
    hasPagesRouter: false,
  },
  codebase: {
    stack: {
      framework: 'next',
      frameworkVersion: '15.4.10',
      hasAppRouter: true,
      hasPagesRouter: false,
    },
  },
};

test('support topics: every checked-in topic passes schema and citation validation', async () => {
  const result = await validateSupportTopics();
  assert.deepEqual(result.errors, []);
  assert.ok(result.topics.length >= 10, 'initial topic pack should cover more than a thin three-topic slice');
});

test('supportTopicSubset: selects uncached-route topics deterministically', async () => {
  const topics = await supportTopicSubset({
    candidate: { kind: 'uncached_route', route: '/api/products', evidence: { patterns: [] } },
    signals: next15Signals,
  });
  assert.deepEqual(
    topics.map((t) => t.id),
    [
      'cdn-cache-auth-safety',
      'next-route-handler-get-cache-defaults',
      'next-fetch-revalidate-floor',
    ],
  );
});

test('supportTopicSubset: filters version-specific topics out for older Next.js versions', async () => {
  const topics = await supportTopicSubset({
    candidate: { kind: 'uncached_route', route: '/api/products' },
    signals: {
      stack: { framework: 'next', frameworkVersion: '13.5.0', hasAppRouter: true },
      codebase: { stack: { framework: 'next', frameworkVersion: '13.5.0', hasAppRouter: true } },
    },
  });
  assert.ok(topics.some((t) => t.id === 'cdn-cache-auth-safety'));
  assert.ok(!topics.some((t) => t.id === 'nextjs-version-cache-semantics'));
});

test('supportTopicSubset: does not leak unrelated topics into a candidate brief', async () => {
  const topics = await supportTopicSubset({
    candidate: { kind: 'cwv_poor', route: '/pricing' },
    signals: next15Signals,
  });
  assert.deepEqual(topics.map((t) => t.id), ['core-web-vitals-client-bottlenecks']);
  assert.ok(!topics.some((t) => t.id.includes('cache')));
});

test('supportTopicSubset: omits Next-only external API topics for non-Next stacks', async () => {
  const topics = await supportTopicSubset({
    candidate: { kind: 'external_api_slow', hostname: 'api.example.com' },
    signals: {
      stack: { framework: 'sveltekit', frameworkVersion: '2.0.0' },
      codebase: { stack: { framework: 'sveltekit', frameworkVersion: '2.0.0' } },
    },
  });
  assert.deepEqual(topics.map((t) => t.id), [
    'external-api-critical-path-platform',
    'runtime-cache-reusable-data',
  ]);
  assert.ok(!topics.some((t) => t.id === 'external-api-critical-path'));
});

test('supportTopicSubset: prefers Next 16 remote-cache guardrail for external API candidates', async () => {
  const topics = await supportTopicSubset({
    candidate: { kind: 'external_api_slow', hostname: 'api.example.com' },
    signals: {
      stack: { framework: 'next', frameworkVersion: '16.0.0', hasAppRouter: true },
      codebase: { stack: { framework: 'next', frameworkVersion: '16.0.0', hasAppRouter: true } },
    },
  });
  assert.deepEqual(topics.map((t) => t.id), [
    'external-api-critical-path',
    'use-cache-remote-shared-origin-data',
    'external-api-critical-path-platform',
  ]);
});

test('renderSupportTopics: renders bounded investigation guardrails', async () => {
  const topics = await supportTopicSubset({
    candidate: { kind: 'middleware_heavy', scope: 'account' },
    signals: next15Signals,
  });
  const md = renderSupportTopics(topics).join('\n');
  assert.match(md, /## Support topics \(investigation guardrails\)/);
  assert.match(md, /Middleware edge cost/);
  assert.match(md, /They do not create recommendations/);
});

test('supportTopicSubset: selects metric-specific Core Web Vitals guardrails', async () => {
  const lcpTopics = await supportTopicSubset({
    candidate: { kind: 'cwv_poor', route: '/pricing', evidence: { issues: [{ metric: 'LCP', value: 3200 }] } },
    signals: { stack: { framework: 'next', frameworkVersion: '16.0.0', hasAppRouter: true } },
  });
  assert.deepEqual(lcpTopics.map((t) => t.id), [
    'core-web-vitals-client-bottlenecks',
    'next-image-lcp-preload-sizes',
    'next-script-third-party-strategy',
  ]);

  const clsTopics = await supportTopicSubset({
    candidate: { kind: 'cwv_poor', route: '/pricing', evidence: { issues: [{ metric: 'CLS', value: 0.2 }] } },
    signals: { stack: { framework: 'next', frameworkVersion: '16.0.0', hasAppRouter: true } },
  });
  assert.deepEqual(clsTopics.map((t) => t.id), [
    'core-web-vitals-client-bottlenecks',
    'next-font-cls-self-hosting',
  ]);
});

test('supportTopicSubset: selects route-pattern topics only for matching routes', async () => {
  const notFoundTopics = await supportTopicSubset({
    candidate: { kind: 'uncached_route', route: '/404', evidence: { patterns: [] } },
    signals: next15Signals,
  });
  assert.ok(notFoundTopics.some((t) => t.id === 'not-found-catchall-request-waste'));

  const normalTopics = await supportTopicSubset({
    candidate: { kind: 'uncached_route', route: '/api/products', evidence: { patterns: [] } },
    signals: next15Signals,
  });
  assert.ok(!normalTopics.some((t) => t.id === 'not-found-catchall-request-waste'));
});

test('supportTopicSubset: selects Workflow stream guardrails only for stream-shaped slow routes', async () => {
  const streamTopics = await supportTopicSubset({
    candidate: { kind: 'slow_route', route: '/api/projects/ai/chat/[id]/stream' },
    signals: next15Signals,
  });
  assert.equal(streamTopics[0].id, 'workflow-resumable-stream-routes');

  const normalTopics = await supportTopicSubset({
    candidate: { kind: 'slow_route', route: '/api/projects' },
    signals: next15Signals,
  });
  assert.ok(!normalTopics.some((t) => t.id === 'workflow-resumable-stream-routes'));
});

test('supportTopicSubset: keeps durable Workflow offload scoped to route errors', async () => {
  const routeErrorTopics = await supportTopicSubset({
    candidate: { kind: 'route_errors', route: '/api/import' },
    signals: next15Signals,
  });
  assert.ok(routeErrorTopics.some((t) => t.id === 'route-error-durable-offload'));

  const slowTopics = await supportTopicSubset({
    candidate: { kind: 'slow_route', route: '/api/import' },
    signals: next15Signals,
  });
  assert.ok(!slowTopics.some((t) => t.id === 'route-error-durable-offload'));
});

test('supportTopicSubset: selects high-impact framework topics for SvelteKit, Nuxt, and Astro', async () => {
  const svelteTopics = await supportTopicSubset({
    candidate: { kind: 'uncached_route', route: '/blog' },
    signals: { stack: { framework: 'sveltekit', frameworkVersion: '2.0.0' } },
  });
  assert.ok(svelteTopics.some((t) => t.id === 'sveltekit-isr-prerender-safety'));

  const nuxtTopics = await supportTopicSubset({
    candidate: { kind: 'uncached_route', route: '/blog' },
    signals: { stack: { framework: 'nuxt', frameworkVersion: '3.12.0' } },
  });
  assert.ok(nuxtTopics.some((t) => t.id === 'nuxt-route-rules-cache-isr'));

  const astroTopics = await supportTopicSubset({
    candidate: { kind: 'rendering_candidate', route: '/blog' },
    signals: { stack: { framework: 'astro', frameworkVersion: '4.0.0' } },
  });
  assert.deepEqual(astroTopics.map((t) => t.id), ['astro-output-mode-and-isr']);
});

test('supportTopicSubset: selects Cache Components for Next 16 rendering candidates', async () => {
  const topics = await supportTopicSubset({
    candidate: { kind: 'rendering_candidate', route: '/products/[slug]' },
    signals: {
      stack: { framework: 'next', frameworkVersion: '16.0.0', hasAppRouter: true },
      codebase: { stack: { framework: 'next', frameworkVersion: '16.0.0', hasAppRouter: true } },
    },
  });
  assert.deepEqual(topics.map((t) => t.id), [
    'cache-components-static-shell-boundaries',
    'dynamic-rendering-traps',
  ]);
});

test('parseSupportTopic: rejects non-JSON array frontmatter', () => {
  assert.throws(() => parseSupportTopic(`---
id: bad-topic
title: Bad Topic
status: active
candidateKinds: [uncached_route]
frameworks: ["*"]
priority: 1
citations: ["https://vercel.com/docs/caching/cdn-cache"]
maxBriefChars: 500
---

## Investigation Brief
x

## Evidence To Check
x

## Do Not Recommend When
x

## Verification
x
`, '/tmp/bad-topic.md'), /strict JSON array syntax/);
});

test('loadSupportTopics: stable sorted source order', async () => {
  const topics = await loadSupportTopics({ includeDraft: true });
  const ids = topics.map((t) => t.id);
  assert.deepEqual(ids, [...ids].sort());
});