ui-steps-api-servers.test.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /**
  2. * `GET /api/steps` on servers: an Express API, a NestJS API, a FastAPI service
  3. * and a Spring controller, in one indexed fixture, shaped to cross every
  4. * boundary an endpoint's picture has — the request and what runs before the
  5. * handler, the database, a queue, an email, and the responses with their
  6. * status codes. Mirrors `ui-steps-api.test.ts` (the mobile app).
  7. */
  8. import { describe, it, expect, beforeAll, afterAll } from 'vitest';
  9. import * as fs from 'fs';
  10. import * as os from 'os';
  11. import * as path from 'path';
  12. import { CodeGraph } from '../src';
  13. import { initGrammars, loadAllGrammars } from '../src/extraction/grammars';
  14. import { buildSteps, projectKind } from '../src/ui-server/api/steps';
  15. import { routeRoots } from '../src/ui-server/api/route-roots';
  16. let tmpDir: string;
  17. let cg: CodeGraph;
  18. function write(rel: string, content: string): void {
  19. const full = path.join(tmpDir, rel);
  20. fs.mkdirSync(path.dirname(full), { recursive: true });
  21. fs.writeFileSync(full, content);
  22. }
  23. beforeAll(async () => {
  24. await initGrammars();
  25. await loadAllGrammars();
  26. tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'cg-ui-steps-servers-'));
  27. write(
  28. 'package.json',
  29. JSON.stringify({ name: 'api', dependencies: { express: '4', '@nestjs/core': '10', '@nestjs/common': '10', bullmq: '5', '@prisma/client': '5', typeorm: '0.3' } })
  30. );
  31. // ---- Express: a named handler behind middleware, and an inline handler.
  32. write('src/server/db.ts', "import { PrismaClient } from '@prisma/client'\nexport const prisma = new PrismaClient()\n");
  33. write('src/server/queue.ts', "import { Queue } from 'bullmq'\nexport const emailQueue = new Queue('email')\n");
  34. write('src/server/errors.ts', 'export class NotFoundError extends Error {}\n');
  35. write('src/server/auth.ts', 'export function authenticate(req, res, next) {\n next()\n}\n');
  36. write('src/server/validate.ts', 'export function validate(schema) {\n return (req, res, next) => next()\n}\n');
  37. write(
  38. 'src/server/users.service.ts',
  39. "import { prisma } from './db'\n" +
  40. "import { emailQueue } from './queue'\n" +
  41. "import { NotFoundError } from './errors'\n" +
  42. 'export async function createUser(req, res) {\n' +
  43. ' const user = await prisma.user.create({ data: { email: req.body.email, name: req.body.name } })\n' +
  44. " await emailQueue.add('welcome', { userId: user.id })\n" +
  45. ' if (!user.verified) {\n' +
  46. ' await sendVerification(user)\n' +
  47. ' }\n' +
  48. ' res.status(201).json(user)\n' +
  49. '}\n' +
  50. 'export async function getUser(id: string) {\n' +
  51. ' const user = await prisma.user.findUnique({ where: { id } })\n' +
  52. " if (!user) throw new NotFoundError('no such user')\n" +
  53. ' return user\n' +
  54. '}\n' +
  55. 'async function sendVerification(user) {\n' +
  56. ' await transporter.sendMail({ to: user.email })\n' +
  57. '}\n'
  58. );
  59. write(
  60. 'src/server/users.routes.ts',
  61. "import { Router } from 'express'\n" +
  62. "import { authenticate } from './auth'\n" +
  63. "import { validate } from './validate'\n" +
  64. "import { createUser, getUser } from './users.service'\n" +
  65. 'const router = Router()\n' +
  66. "router.post('/users', authenticate, validate(userSchema), createUser)\n" +
  67. "router.get('/users/:id', authenticate, async (req, res) => {\n" +
  68. ' const user = await getUser(req.params.id)\n' +
  69. ' res.json(user)\n' +
  70. '})\n' +
  71. 'export default router\n'
  72. );
  73. // ---- NestJS: guards on the class and the method, DI into a service, a queue consumer.
  74. write(
  75. 'src/nest/cats.service.ts',
  76. "import { Injectable } from '@nestjs/common'\n" +
  77. "import { InjectRepository } from '@nestjs/typeorm'\n" +
  78. "import { Repository } from 'typeorm'\n" +
  79. "import { InjectQueue } from '@nestjs/bullmq'\n" +
  80. "import { Queue } from 'bullmq'\n" +
  81. "import { Cat } from './cat.entity'\n" +
  82. '@Injectable()\n' +
  83. 'export class CatsService {\n' +
  84. ' constructor(\n' +
  85. ' @InjectRepository(Cat) private readonly catsRepository: Repository<Cat>,\n' +
  86. " @InjectQueue('cats') private readonly catsQueue: Queue\n" +
  87. ' ) {}\n' +
  88. ' async create(dto) {\n' +
  89. ' const cat = await this.catsRepository.save(dto)\n' +
  90. " await this.catsQueue.add('index', { id: cat.id })\n" +
  91. ' return cat\n' +
  92. ' }\n' +
  93. ' async findOne(id: string) {\n' +
  94. ' return this.catsRepository.findOne({ where: { id } })\n' +
  95. ' }\n' +
  96. '}\n'
  97. );
  98. write('src/nest/cat.entity.ts', "import { Entity } from 'typeorm'\n@Entity()\nexport class Cat {\n id: string\n}\n");
  99. write(
  100. 'src/nest/cats.controller.ts',
  101. "import { Controller, Get, Post, Body, Param, UseGuards, NotFoundException } from '@nestjs/common'\n" +
  102. "import { AuthGuard } from '@nestjs/passport'\n" +
  103. "import { CatsService } from './cats.service'\n" +
  104. "import { RolesGuard } from './roles.guard'\n" +
  105. "@Controller('cats')\n" +
  106. "@UseGuards(AuthGuard('jwt'))\n" +
  107. 'export class CatsController {\n' +
  108. ' constructor(private readonly catsService: CatsService) {}\n' +
  109. ' @Post()\n' +
  110. ' @UseGuards(RolesGuard)\n' +
  111. ' async create(@Body() dto: CreateCatDto) {\n' +
  112. ' return this.catsService.create(dto)\n' +
  113. ' }\n' +
  114. " @Get(':id')\n" +
  115. " async findOne(@Param('id') id: string) {\n" +
  116. ' const cat = await this.catsService.findOne(id)\n' +
  117. " if (!cat) throw new NotFoundException('no cat')\n" +
  118. ' return cat\n' +
  119. ' }\n' +
  120. '}\n'
  121. );
  122. write('src/nest/roles.guard.ts', "import { Injectable } from '@nestjs/common'\n@Injectable()\nexport class RolesGuard {\n canActivate() { return true }\n}\n");
  123. write(
  124. 'src/nest/cats.processor.ts',
  125. "import { Processor, Process } from '@nestjs/bull'\n" +
  126. "@Processor('cats')\n" +
  127. 'export class CatsProcessor {\n' +
  128. " @Process('index')\n" +
  129. ' async handleIndex(job) {\n' +
  130. ' await searchClient.index(job.data)\n' +
  131. ' }\n' +
  132. '}\n'
  133. );
  134. // ---- FastAPI: a dependency on the route, SQLModel, an HTTPException, a Celery task.
  135. write(
  136. 'api/items.py',
  137. 'from fastapi import APIRouter, Depends, HTTPException\n' +
  138. 'from sqlmodel import select\n' +
  139. 'from .deps import get_current_user, SessionDep\n' +
  140. 'from .models import Item, ItemCreate\n' +
  141. 'from .tasks import send_welcome\n' +
  142. '\n' +
  143. 'router = APIRouter()\n' +
  144. '\n' +
  145. '@router.post("/items", dependencies=[Depends(get_current_user)])\n' +
  146. 'def create_item(session: SessionDep, item_in: ItemCreate):\n' +
  147. ' item = Item.model_validate(item_in)\n' +
  148. ' session.add(item)\n' +
  149. ' session.commit()\n' +
  150. ' if item.price < 0:\n' +
  151. ' raise HTTPException(status_code=422, detail="bad price")\n' +
  152. ' send_welcome.delay(item.id)\n' +
  153. ' return item\n'
  154. );
  155. write('api/deps.py', 'def get_current_user():\n return None\n\nSessionDep = None\n');
  156. write('api/models.py', 'class Item:\n pass\n\nclass ItemCreate:\n pass\n');
  157. write('api/tasks.py', 'from celery import shared_task\n\n@shared_task\ndef send_welcome(item_id):\n return item_id\n');
  158. write('api/main.py', 'from fastapi import FastAPI\nfrom .items import router\napp = FastAPI()\napp.include_router(router)\n');
  159. write('requirements.txt', 'fastapi\nsqlmodel\ncelery\n');
  160. // ---- Spring: a repository typed on a field, ResponseEntity replies, a guard annotation.
  161. write(
  162. 'src/main/java/demo/OwnerController.java',
  163. 'package demo;\n' +
  164. 'import org.springframework.web.bind.annotation.*;\n' +
  165. 'import org.springframework.http.*;\n' +
  166. '@RestController\n' +
  167. '@RequestMapping("/owners")\n' +
  168. 'public class OwnerController {\n' +
  169. ' private final OwnerRepository owners;\n' +
  170. ' public OwnerController(OwnerRepository owners) { this.owners = owners; }\n' +
  171. ' @PostMapping("/new")\n' +
  172. ' @PreAuthorize("hasRole(\'ADMIN\')")\n' +
  173. ' public ResponseEntity<Owner> create(@RequestBody Owner owner) {\n' +
  174. ' if (owner.getName() == null) {\n' +
  175. ' return ResponseEntity.badRequest().build();\n' +
  176. ' }\n' +
  177. ' Owner saved = owners.save(owner);\n' +
  178. ' return ResponseEntity.status(HttpStatus.CREATED).body(saved);\n' +
  179. ' }\n' +
  180. '}\n'
  181. );
  182. write(
  183. 'src/main/java/demo/OwnerRepository.java',
  184. 'package demo;\nimport org.springframework.data.jpa.repository.JpaRepository;\npublic interface OwnerRepository extends JpaRepository<Owner, Integer> {\n}\n'
  185. );
  186. write('src/main/java/demo/Owner.java', 'package demo;\npublic class Owner {\n private String name;\n public String getName() { return name; }\n}\n');
  187. cg = CodeGraph.initSync(tmpDir);
  188. await cg.indexAll();
  189. });
  190. afterAll(() => {
  191. cg?.close();
  192. if (tmpDir) fs.rmSync(tmpDir, { recursive: true, force: true });
  193. });
  194. const q = (params: Record<string, string>) => new URLSearchParams(params);
  195. const route = (name: string) => {
  196. const r = cg.getNodesByKind('route').find((r) => r.name === name);
  197. if (!r) throw new Error(`no route ${name}: ${cg.getNodesByKind('route').map((r) => r.name).join(', ')}`);
  198. return r;
  199. };
  200. const effect = (p: Awaited<ReturnType<typeof buildSteps>>, category: string) => p.steps.find((s) => s.kind === 'effect' && s.effect?.category === category);
  201. describe('route roots', () => {
  202. it('names the handler an API route runs, the route itself for an inline handler', () => {
  203. const roots = routeRoots(cg, cg.getNodesByKind('route'));
  204. expect(roots.get(route('POST /users').id)).toMatchObject({ inline: false, node: { name: 'createUser' } });
  205. expect(roots.get(route('GET /users/:id').id)).toMatchObject({ inline: true });
  206. expect(roots.get(route('POST /cats').id)?.node.qualifiedName).toContain('CatsController');
  207. expect(roots.get(route('POST /items').id)?.node.name).toBe('create_item');
  208. expect(roots.get(route('POST /owners/new').id)?.node.name).toBe('create');
  209. });
  210. it('calls the project an API', () => {
  211. expect(projectKind(cg.getNodesByKind('route'), 0)).toBe('api');
  212. });
  213. });
  214. describe('Express', () => {
  215. it('draws the handler’s database write, the queue job, the email, and the 201 — after the middleware', async () => {
  216. const p = await buildSteps(cg, tmpDir, q({ anchor: route('POST /users').id }));
  217. expect(p.project).toBe('api');
  218. const anchor = p.steps.find((s) => s.anchor)!;
  219. expect(anchor.kind).toBe('screen');
  220. expect(anchor.sub).toBe('createUser');
  221. expect(anchor.screen).toMatchObject({ path: 'POST /users', endpoint: true, inline: false, component: { name: 'createUser' } });
  222. expect(anchor.trigger).toEqual({ kind: 'request', name: 'POST', of: '/users', in: 'users.routes.ts', after: ['authenticate', 'validate(…)'] });
  223. const db = effect(p, 'database')!;
  224. expect(db.label).toBe('prisma.user.create({ data })');
  225. expect(db.effect).toMatchObject({ model: 'user', access: 'write', by: { name: 'createUser' } });
  226. expect(db.sub).toBe('database · user · write · createUser');
  227. const queue = effect(p, 'queue')!;
  228. expect(queue.label).toBe("emailQueue.add('welcome', { userId })");
  229. const mail = effect(p, 'email')!;
  230. expect(mail.label).toBe('transporter.sendMail({ to })');
  231. const mailLink = p.links.find((l) => l.to === mail.id)!;
  232. expect(mailLink.via.map((v) => v.name)).toEqual(['sendVerification']);
  233. expect(mailLink.when).toBe('!user.verified');
  234. const res = effect(p, 'response')!;
  235. expect(res.label).toBe('201');
  236. expect(res.effect?.statuses).toEqual([201]);
  237. const resLink = p.links.find((l) => l.to === res.id)!;
  238. expect(resLink.sites[0]).toMatchObject({ text: 'res.status(201).json', args: 'user', status: 201 });
  239. });
  240. it('walks an inline handler as the route itself, into the service’s read and its 404', async () => {
  241. const p = await buildSteps(cg, tmpDir, q({ anchor: route('GET /users/:id').id }));
  242. const anchor = p.steps.find((s) => s.anchor)!;
  243. expect(anchor.sub).toBe('inline handler · users.routes.ts');
  244. expect(anchor.trigger).toMatchObject({ kind: 'request', name: 'GET', of: '/users/:id', after: ['authenticate'] });
  245. const db = effect(p, 'database')!;
  246. expect(db.effect).toMatchObject({ model: 'user', access: 'read', by: { name: 'getUser' } });
  247. const res = effect(p, 'response')!;
  248. expect(res.label).toBe('404');
  249. const resLink = p.links.find((l) => l.to === res.id)!;
  250. expect(resLink.sites[0]).toMatchObject({ text: 'NotFoundError', status: 404, when: '!user' });
  251. expect(resLink.via.map((v) => v.name)).toEqual(['getUser']);
  252. });
  253. });
  254. describe('NestJS', () => {
  255. it('reads the guards on the class and the method, follows DI into the repository and the queue', async () => {
  256. const p = await buildSteps(cg, tmpDir, q({ anchor: route('POST /cats').id }));
  257. const anchor = p.steps.find((s) => s.anchor)!;
  258. expect(anchor.sub).toBe('create');
  259. expect(anchor.trigger).toEqual({ kind: 'request', name: 'POST', of: '/cats', in: 'cats.controller.ts', after: ["UseGuards(AuthGuard('jwt'))", 'UseGuards(RolesGuard)'] });
  260. const db = effect(p, 'database')!;
  261. expect(db.label).toBe('this.catsRepository.save(dto)');
  262. expect(db.effect).toMatchObject({ model: 'cats', access: 'write', by: { name: 'create' } });
  263. const dbLink = p.links.find((l) => l.to === db.id)!;
  264. expect(dbLink.via.map((v) => v.name)).toEqual(['create']);
  265. const queue = effect(p, 'queue')!;
  266. expect(queue.label).toBe("this.catsQueue.add('index', { id })");
  267. });
  268. it('a thrown exception is the 404 the request gets', async () => {
  269. const p = await buildSteps(cg, tmpDir, q({ anchor: route('GET /cats/:id').id }));
  270. const res = effect(p, 'response')!;
  271. expect(res.label).toBe('404');
  272. const resLink = p.links.find((l) => l.to === res.id)!;
  273. expect(resLink.sites[0]).toMatchObject({ text: 'NotFoundException', args: "'no cat'", status: 404, when: '!cat' });
  274. expect(effect(p, 'database')?.effect).toMatchObject({ access: 'read' });
  275. });
  276. it('a queue consumer says the job that fires it', async () => {
  277. const p = await buildSteps(cg, tmpDir, q({ symbol: 'handleIndex' }));
  278. expect(p.steps.find((s) => s.anchor)?.trigger).toEqual({ kind: 'decorator', name: 'Process', of: "'index'", in: 'cats.processor.ts' });
  279. });
  280. });
  281. describe('FastAPI', () => {
  282. it('reads the dependency on the route, the session writes, the 422 and the Celery task', async () => {
  283. const p = await buildSteps(cg, tmpDir, q({ anchor: route('POST /items').id }));
  284. const anchor = p.steps.find((s) => s.anchor)!;
  285. expect(anchor.sub).toBe('create_item');
  286. expect(anchor.trigger).toEqual({ kind: 'request', name: 'POST', of: '/items', in: 'items.py', after: ['Depends(get_current_user)'] });
  287. const db = effect(p, 'database')!;
  288. expect(db.effect?.apis).toEqual(['session.add', 'session.commit']);
  289. expect(db.effect).toMatchObject({ access: 'write' });
  290. const res = effect(p, 'response')!;
  291. expect(res.label).toBe('422');
  292. const resLink = p.links.find((l) => l.to === res.id)!;
  293. expect(resLink.sites[0]).toMatchObject({ text: 'HTTPException', args: 'status_code=422, detail="bad price"', status: 422, when: 'item.price < 0' });
  294. const queue = effect(p, 'queue')!;
  295. expect(queue.label).toBe('send_welcome.delay(item.id)');
  296. });
  297. });
  298. describe('Spring', () => {
  299. it('types the repository off the field, reads the annotation guard, and both replies with their codes', async () => {
  300. const p = await buildSteps(cg, tmpDir, q({ anchor: route('POST /owners/new').id }));
  301. const anchor = p.steps.find((s) => s.anchor)!;
  302. expect(anchor.sub).toBe('create');
  303. expect(anchor.trigger).toEqual({ kind: 'request', name: 'POST', of: '/owners/new', in: 'OwnerController.java', after: ["PreAuthorize(\"hasRole('ADMIN')\")"] });
  304. const db = effect(p, 'database')!;
  305. expect(db.label).toBe('owners.save(owner)');
  306. expect(db.effect).toMatchObject({ model: 'Owner', access: 'write' });
  307. const dbLink = p.links.find((l) => l.to === db.id)!;
  308. expect(dbLink.when).toBe('owner.getName() != null');
  309. const res = effect(p, 'response')!;
  310. expect(res.label).toBe('201 · 400');
  311. const rows = p.links.find((l) => l.to === res.id)!.sites.map((s) => [s.status, s.when]);
  312. expect(rows).toEqual([
  313. [400, 'owner.getName() == null'],
  314. [201, 'owner.getName() != null'],
  315. ]);
  316. });
  317. });