|
| 1 | +import { describe, expect, it } from "@jest/globals"; |
| 2 | +import { promises as fs } from "node:fs"; |
| 3 | +import path from "node:path"; |
| 4 | + |
| 5 | +import { pruneRoutes } from "../../src/typescript-generator/prune.js"; |
| 6 | +import { withTemporaryFiles } from "../lib/with-temporary-files.ts"; |
| 7 | + |
| 8 | +describe("pruneRoutes", () => { |
| 9 | + it("removes a route file that is not in the OpenAPI spec", async () => { |
| 10 | + await withTemporaryFiles( |
| 11 | + { |
| 12 | + "routes/pet/{id}.ts": "export const GET = () => ({ status: 200 });", |
| 13 | + "routes/pet/{name}.ts": "export const GET = () => ({ status: 200 });", |
| 14 | + }, |
| 15 | + async (basePath, { path: getPath }) => { |
| 16 | + const expectedPaths = ["/pet/{id}"]; |
| 17 | + const count = await pruneRoutes(basePath, expectedPaths); |
| 18 | + |
| 19 | + expect(count).toBe(1); |
| 20 | + await expect( |
| 21 | + fs.access(getPath("routes/pet/{id}.ts")), |
| 22 | + ).resolves.toBeUndefined(); |
| 23 | + await expect( |
| 24 | + fs.access(getPath("routes/pet/{name}.ts")), |
| 25 | + ).rejects.toThrow(); |
| 26 | + }, |
| 27 | + ); |
| 28 | + }); |
| 29 | + |
| 30 | + it("keeps context files even when not in the spec", async () => { |
| 31 | + await withTemporaryFiles( |
| 32 | + { |
| 33 | + "routes/_.context.ts": "export class Context {}", |
| 34 | + "routes/pet/_.context.ts": "export class Context {}", |
| 35 | + "routes/pet/{id}.ts": "export const GET = () => ({ status: 200 });", |
| 36 | + }, |
| 37 | + async (basePath, { path: getPath }) => { |
| 38 | + const expectedPaths = ["/pet/{id}"]; |
| 39 | + const count = await pruneRoutes(basePath, expectedPaths); |
| 40 | + |
| 41 | + expect(count).toBe(0); |
| 42 | + await expect( |
| 43 | + fs.access(getPath("routes/_.context.ts")), |
| 44 | + ).resolves.toBeUndefined(); |
| 45 | + await expect( |
| 46 | + fs.access(getPath("routes/pet/_.context.ts")), |
| 47 | + ).resolves.toBeUndefined(); |
| 48 | + }, |
| 49 | + ); |
| 50 | + }); |
| 51 | + |
| 52 | + it("removes empty directories after pruning", async () => { |
| 53 | + await withTemporaryFiles( |
| 54 | + { |
| 55 | + "routes/old/{id}.ts": "export const GET = () => ({ status: 200 });", |
| 56 | + }, |
| 57 | + async (basePath, { path: getPath }) => { |
| 58 | + const expectedPaths = []; |
| 59 | + await pruneRoutes(basePath, expectedPaths); |
| 60 | + |
| 61 | + await expect(fs.access(getPath("routes/old"))).rejects.toThrow(); |
| 62 | + }, |
| 63 | + ); |
| 64 | + }); |
| 65 | + |
| 66 | + it("does not remove directories that still contain context files", async () => { |
| 67 | + await withTemporaryFiles( |
| 68 | + { |
| 69 | + "routes/old/{id}.ts": "export const GET = () => ({ status: 200 });", |
| 70 | + "routes/old/_.context.ts": "export class Context {}", |
| 71 | + }, |
| 72 | + async (basePath, { path: getPath }) => { |
| 73 | + const expectedPaths = []; |
| 74 | + await pruneRoutes(basePath, expectedPaths); |
| 75 | + |
| 76 | + await expect(fs.access(getPath("routes/old"))).resolves.toBeUndefined(); |
| 77 | + await expect( |
| 78 | + fs.access(getPath("routes/old/_.context.ts")), |
| 79 | + ).resolves.toBeUndefined(); |
| 80 | + }, |
| 81 | + ); |
| 82 | + }); |
| 83 | + |
| 84 | + it("handles the root path '/'", async () => { |
| 85 | + await withTemporaryFiles( |
| 86 | + { |
| 87 | + "routes/index.ts": "export const GET = () => ({ status: 200 });", |
| 88 | + "routes/old.ts": "export const GET = () => ({ status: 200 });", |
| 89 | + }, |
| 90 | + async (basePath, { path: getPath }) => { |
| 91 | + const expectedPaths = ["/"]; |
| 92 | + const count = await pruneRoutes(basePath, expectedPaths); |
| 93 | + |
| 94 | + expect(count).toBe(1); |
| 95 | + await expect( |
| 96 | + fs.access(getPath("routes/index.ts")), |
| 97 | + ).resolves.toBeUndefined(); |
| 98 | + await expect(fs.access(getPath("routes/old.ts"))).rejects.toThrow(); |
| 99 | + }, |
| 100 | + ); |
| 101 | + }); |
| 102 | + |
| 103 | + it("returns 0 when all files match the spec", async () => { |
| 104 | + await withTemporaryFiles( |
| 105 | + { |
| 106 | + "routes/pet/{id}.ts": "export const GET = () => ({ status: 200 });", |
| 107 | + "routes/pet.ts": "export const GET = () => ({ status: 200 });", |
| 108 | + }, |
| 109 | + async (basePath) => { |
| 110 | + const expectedPaths = ["/pet/{id}", "/pet"]; |
| 111 | + const count = await pruneRoutes(basePath, expectedPaths); |
| 112 | + |
| 113 | + expect(count).toBe(0); |
| 114 | + }, |
| 115 | + ); |
| 116 | + }); |
| 117 | + |
| 118 | + it("returns 0 when the routes directory does not exist", async () => { |
| 119 | + await withTemporaryFiles({}, async (basePath) => { |
| 120 | + const count = await pruneRoutes(basePath, ["/pet/{id}"]); |
| 121 | + |
| 122 | + expect(count).toBe(0); |
| 123 | + }); |
| 124 | + }); |
| 125 | +}); |
0 commit comments