基于 SOC 职业分类
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
直接命令不会经过审查 Prompt;运行前请先检查来源。
npx skills add https://github.com/dev-hann/song --skill api-testing命令会保持在同一行。复制前请横向滚动并检查完整内容。
想先保存到本地?可下载 SkillsMP 当前能够提供的文件。
正在显示 SKILL.md
Runs PM-Designer-Doc triple review protocol to reach consensus on feature specs before implementation. Includes domain documentation consistency check. Use before implementing any new screen, component, or significant feature.
Create Server and Client React components with proper props, state, and patterns
Enforce docs ↔ code consistency before and after implementation. Launches doc-reviewer agent to cross-check domain documentation against actual codebase, detects drift, and ensures documentation updates accompany code changes.
| name | api-testing |
| description | Test API routes with proper validation, mocking, and error handling |
| license | MIT |
| compatibility | opencode |
| metadata | {"category":"testing","complexity":"intermediate"} |
Use this when you need to:
I'll ask clarifying questions if:
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { GET } from '@/app/api/youtube/search/route';
describe('GET /api/youtube/search', () => {
let mockRequest: any;
beforeEach(() => {
vi.clearAllMocks();
mockRequest = {
url: new URL('http://localhost:3000/api/youtube/search'),
nextUrl: { searchParams: new URLSearchParams() }
};
});
it('should return 400 for missing query', async () => {
mockRequest.url.searchParams.get.mockReturnValueOnce(null);
const response = await GET(mockRequest);
expect(response.status).toBe(400);
expect(await response.json()).toEqual({
error: 'Query parameter "q" is required'
});
});
it('should return search results for valid query', async () => {
mockRequest....();
mockSearch = vi.().({
: [mockVideo],
:
});
vi.(, ({
: vi.().({ : mockSearch })
}));
response = (mockRequest);
data = response.();
(response.).();
(data.).();
});
});
import { vi, beforeEach } from 'vitest';
describe('API Route with External Dependencies', () => {
beforeEach(() => {
// Clear all mocks before each test
vi.clearAllMocks();
// Mock YouTube.js library
vi.mock('youtubei.js', () => ({
Innertube: {
create: vi.fn().mockResolvedValue({
search: vi.fn(),
getInfo: vi.fn()
})
}
}));
// Mock internal library
vi.mock('@/lib/youtube', () => ({
getInnertube: vi.fn(),
getYouTubeSession: vi.fn()
}));
});
it('should call getInnertube with correct config', async () => {
const mockRequest = createMockRequest({ q: 'test' });
await GET(mockRequest);
const { getInnertube } = await import('@/lib/youtube');
expect(getInnertube).toHaveBeenCalled();
});
});
describe('Error Handling', () => {
it('should return 500 on YouTube.js error', async () => {
vi.mock('@/lib/youtube', () => ({
getInnertube: vi.fn().mockRejectedValue(new Error('API Error'))
}));
const mockRequest = createMockRequest({ q: 'test' });
const response = await GET(mockRequest);
expect(response.status).toBe(500);
expect(await response.json()).toEqual({
error: 'Internal server error'
});
});
it('should return 404 for invalid video ID', async () => {
const mockGetInfo = vi.fn().mockResolvedValue(null);
vi.mock('@/lib/youtube', () => ({
getInnertube: vi.fn().mockResolvedValue({ getInfo: mockGetInfo })
}));
const mockRequest = createMockRequest({ : });
response = (mockRequest);
(response.).();
});
});
describe('Query Parameters', () => {
it('should parse query parameter', async () => {
const mockRequest = createMockRequest({ q: 'test' });
const response = await GET(mockRequest);
const data = await response.json();
expect(response.status).toBe(200);
});
it('should handle multiple parameters', async () => {
const mockRequest = createMockRequest({
q: 'test',
filter: 'video',
limit: '10'
});
const response = await GET(mockRequest);
const data = await response.json();
expect(response.status).toBe(200);
});
it('should validate parameter types', async () => {
const mockRequest = createMockRequest({ limit: 'invalid' });
const response = (mockRequest);
(response.).();
( response.()).({
:
});
});
});
describe('Response Format', () => {
it('should return JSON response', async () => {
const mockRequest = createMockRequest({ q: 'test' });
const response = await GET(mockRequest);
expect(response.headers.get('content-type')).toContain('application/json');
});
it('should include CORS headers', async () => {
const mockRequest = createMockRequest({ q: 'test' });
const response = await GET(mockRequest);
expect(response.headers.get('access-control-allow-origin')).toBe('*');
});
it('should have correct response structure', async () => {
const mockRequest = createMockRequest({ q: 'test' });
const response = await GET(mockRequest);
const data = await response.json();
(data).();
(.(data.)).();
});
});
// __tests__/utils/api-helpers.ts
export function createMockRequest(params: Record<string, string>): any {
const url = new URL(`http://localhost:3000/api/youtube/search`);
Object.entries(params).forEach(([key, value]) => {
url.searchParams.set(key, value);
});
return {
url,
nextUrl: { searchParams: url.searchParams }
};
}
export function createMockVideo(overrides = {}): Video {
return {
id: 'video123',
type: 'video',
title: 'Test Video',
description: 'Test Description',
duration: 120,
viewCount: 5000,
thumbnail: 'https://example.com/thumb.jpg',
channel: {
id: 'channel123',
name: 'Test Channel',
thumbnail: ''
},
...overrides
};
}
describe('GET /api/youtube/search', () => {
describe('Validation', () => {
it('should require query parameter');
it('should validate query type');
it('should validate optional parameters');
});
describe('Success Cases', () => {
it('should return search results');
it('should handle pagination');
it('should filter by type');
});
describe('Error Cases', () => {
it('should handle YouTube API errors');
it('should handle network errors');
it('should handle timeout errors');
});
describe('Response Format', () => {
it('should return JSON');
it('should include CORS headers');
it('should have correct structure');
});
});
describe('AAA Pattern Example', () => {
it('should return search results', async () => {
// Arrange
const mockRequest = createMockRequest({ q: 'test' });
const mockSearch = vi.fn().mockResolvedValue({
results: [createMockVideo()],
has_continuation: false
});
vi.mock('@/lib/youtube', () => ({
getInnertube: vi.fn().mockResolvedValue({ search: mockSearch })
}));
// Act
const response = await GET(mockRequest);
const data = await response.json();
// Assert
expect(response.status).toBe(200);
expect(data.results).toHaveLength(1);
expect(data.results[0].title).toBe('Test Video');
});
});