@decohere/scaffold 0.0.1-dev.125 → 0.0.1-dev.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (264) hide show
  1. package/package.json +9 -19
  2. package/src/add-api.action.ts +25 -0
  3. package/src/add-api.command.ts +31 -0
  4. package/src/add.command.ts +20 -0
  5. package/src/add.module.ts +11 -0
  6. package/src/cli.ts +4 -0
  7. package/src/create.command.ts +20 -0
  8. package/src/create.module.ts +11 -0
  9. package/src/domain/prompt.ts +50 -0
  10. package/{types/src/index.d.ts → src/index.ts} +0 -4
  11. package/src/models/compose.model.ts +205 -0
  12. package/src/models/config-yaml.model.ts +83 -0
  13. package/src/models/const.ts +20 -0
  14. package/src/models/dev-container.model.ts +136 -0
  15. package/src/models/docker-image.model.ts +343 -0
  16. package/src/models/env-file.model.ts +45 -0
  17. package/src/models/gitignore.model.ts +35 -0
  18. package/src/models/launch-json.model.ts +60 -0
  19. package/src/models/package-json.model.ts +169 -0
  20. package/src/models/project.model.ts +141 -0
  21. package/{types/src/models/source-file.d.ts → src/models/source-file.ts} +6 -4
  22. package/src/models/tsconfig.model.ts +29 -0
  23. package/src/models/vitest-config.model.ts +26 -0
  24. package/src/models/vscode-settings.model.ts +24 -0
  25. package/src/program.ts +63 -0
  26. package/{types/src/resolve-target-dir.d.ts → src/resolve-target-dir.ts} +4 -1
  27. package/src/services/scaffold.service.ts +89 -0
  28. package/src/version.ts +56 -0
  29. package/src/webapi/api-name.ts +73 -0
  30. package/src/webapi/cli-options.ts +56 -0
  31. package/{types/src/webapi/const.d.ts → src/webapi/const.ts} +21 -9
  32. package/src/webapi/create-api.ts +275 -0
  33. package/src/webapi/devcontainer/host-scripts.ts +35 -0
  34. package/src/webapi/features/auth.feature.ts +454 -0
  35. package/src/webapi/features/auth0-cli.ts +421 -0
  36. package/src/webapi/features/biome-format.ts +34 -0
  37. package/src/webapi/features/database.feature.ts +522 -0
  38. package/src/webapi/features/finalize.feature.ts +112 -0
  39. package/src/webapi/features/postman.feature.ts +287 -0
  40. package/src/webapi/models/api-package-json.model.ts +60 -0
  41. package/src/webapi/models/auth-config.model.ts +36 -0
  42. package/src/webapi/models/controller-source-file.model.ts +232 -0
  43. package/{es6/src/webapi/models/github-ci.model.js → src/webapi/models/github-ci.model.ts} +29 -27
  44. package/src/webapi/models/main-source-file.model.ts +208 -0
  45. package/src/webapi/models/production-dockerfile.model.ts +162 -0
  46. package/src/webapi/models/sample-files.model.ts +45 -0
  47. package/src/webapi/models/webapi-image.model.ts +21 -0
  48. package/src/webapi/postman/hello-openapi.ts +92 -0
  49. package/src/webapi/postman/local-view.ts +195 -0
  50. package/src/webapi/postman/openapi-to-postman.ts +208 -0
  51. package/src/webapi/workspace-readme.ts +192 -0
  52. package/src/webapi.action.ts +22 -0
  53. package/src/webapi.command.ts +41 -0
  54. package/tests/auth-postman.wiring.spec.ts +263 -0
  55. package/tests/auth0-cli.spec.ts +232 -0
  56. package/tests/biome-format.spec.ts +49 -0
  57. package/tests/cli.integration.spec.ts +658 -0
  58. package/tests/compose.model.spec.ts +51 -0
  59. package/tests/container-image.model.spec.ts +162 -0
  60. package/tests/dev-container.model.spec.ts +29 -0
  61. package/tests/devcontainer-base-image.spec.ts +32 -0
  62. package/tests/hello-di-inject.scaffold.spec.ts +18 -0
  63. package/tests/hello-route.contract.spec.ts +94 -0
  64. package/tests/package-json.order.spec.ts +59 -0
  65. package/tests/postman-from-openapi.spec.ts +197 -0
  66. package/tests/seed-devcontainer-templates.ts +40 -0
  67. package/tests/webapi/devcontainer/codebase-rag-mcp.spec.ts +96 -0
  68. package/LICENSE +0 -15
  69. package/commonjs/src/add-api.action.js +0 -37
  70. package/commonjs/src/add-api.command.js +0 -39
  71. package/commonjs/src/add.command.js +0 -22
  72. package/commonjs/src/add.module.js +0 -24
  73. package/commonjs/src/auth0-configure.action.js +0 -28
  74. package/commonjs/src/auth0-configure.command.js +0 -24
  75. package/commonjs/src/auth0-login.action.js +0 -28
  76. package/commonjs/src/auth0-login.command.js +0 -23
  77. package/commonjs/src/auth0.command.js +0 -22
  78. package/commonjs/src/auth0.module.js +0 -28
  79. package/commonjs/src/cli.js +0 -8
  80. package/commonjs/src/create.command.js +0 -22
  81. package/commonjs/src/create.module.js +0 -24
  82. package/commonjs/src/domain/prompt.js +0 -62
  83. package/commonjs/src/index.js +0 -48
  84. package/commonjs/src/models/compose.model.js +0 -258
  85. package/commonjs/src/models/config-yaml.model.js +0 -86
  86. package/commonjs/src/models/const.js +0 -30
  87. package/commonjs/src/models/dev-container.model.js +0 -172
  88. package/commonjs/src/models/docker-image.model.js +0 -277
  89. package/commonjs/src/models/env-file.model.js +0 -48
  90. package/commonjs/src/models/gitignore.model.js +0 -48
  91. package/commonjs/src/models/launch-json.model.js +0 -70
  92. package/commonjs/src/models/package-json.model.js +0 -156
  93. package/commonjs/src/models/project.model.js +0 -132
  94. package/commonjs/src/models/source-file.js +0 -4
  95. package/commonjs/src/models/tsconfig.model.js +0 -32
  96. package/commonjs/src/models/vitest-config.model.js +0 -33
  97. package/commonjs/src/models/vscode-settings.model.js +0 -29
  98. package/commonjs/src/program.js +0 -64
  99. package/commonjs/src/resolve-target-dir.js +0 -17
  100. package/commonjs/src/services/scaffold.service.js +0 -60
  101. package/commonjs/src/version.js +0 -55
  102. package/commonjs/src/webapi/api-name.js +0 -93
  103. package/commonjs/src/webapi/auth0-commands.js +0 -250
  104. package/commonjs/src/webapi/authorize-scopes.js +0 -79
  105. package/commonjs/src/webapi/cli-options.js +0 -99
  106. package/commonjs/src/webapi/const.js +0 -54
  107. package/commonjs/src/webapi/create-api.js +0 -265
  108. package/commonjs/src/webapi/devcontainer/codebase-rag-mcp.js +0 -164
  109. package/commonjs/src/webapi/devcontainer/codebase-rag-mcp.ts +0 -182
  110. package/commonjs/src/webapi/devcontainer/host-scripts.js +0 -50
  111. package/commonjs/src/webapi/devcontainer/templates/codebase-rag.env.example +0 -3
  112. package/commonjs/src/webapi/devcontainer/templates/initialize.sh +0 -169
  113. package/commonjs/src/webapi/devcontainer/templates/post-create.sh +0 -35
  114. package/commonjs/src/webapi/devcontainer/templates/post-start.sh +0 -65
  115. package/commonjs/src/webapi/devcontainer/templates/tty-prompt.sh +0 -38
  116. package/commonjs/src/webapi/features/auth.feature.js +0 -408
  117. package/commonjs/src/webapi/features/auth0-cli.js +0 -472
  118. package/commonjs/src/webapi/features/biome-format.js +0 -47
  119. package/commonjs/src/webapi/features/database.feature.js +0 -433
  120. package/commonjs/src/webapi/features/finalize.feature.js +0 -93
  121. package/commonjs/src/webapi/features/postman.feature.js +0 -322
  122. package/commonjs/src/webapi/models/api-package-json.model.js +0 -58
  123. package/commonjs/src/webapi/models/auth-config.model.js +0 -24
  124. package/commonjs/src/webapi/models/controller-source-file.model.js +0 -190
  125. package/commonjs/src/webapi/models/github-ci.model.js +0 -193
  126. package/commonjs/src/webapi/models/main-source-file.model.js +0 -165
  127. package/commonjs/src/webapi/models/production-dockerfile.model.js +0 -75
  128. package/commonjs/src/webapi/models/sample-files.model.js +0 -58
  129. package/commonjs/src/webapi/models/webapi-image.model.js +0 -25
  130. package/commonjs/src/webapi/postman/hello-openapi.js +0 -173
  131. package/commonjs/src/webapi/postman/local-view.js +0 -182
  132. package/commonjs/src/webapi/postman/openapi-to-postman.js +0 -238
  133. package/commonjs/src/webapi/workspace-readme.js +0 -109
  134. package/commonjs/src/webapi.action.js +0 -28
  135. package/commonjs/src/webapi.command.js +0 -43
  136. package/es6/src/add-api.action.js +0 -27
  137. package/es6/src/add-api.command.js +0 -29
  138. package/es6/src/add.command.js +0 -12
  139. package/es6/src/add.module.js +0 -14
  140. package/es6/src/auth0-configure.action.js +0 -18
  141. package/es6/src/auth0-configure.command.js +0 -14
  142. package/es6/src/auth0-login.action.js +0 -18
  143. package/es6/src/auth0-login.command.js +0 -13
  144. package/es6/src/auth0.command.js +0 -12
  145. package/es6/src/auth0.module.js +0 -18
  146. package/es6/src/cli.js +0 -4
  147. package/es6/src/create.command.js +0 -12
  148. package/es6/src/create.module.js +0 -14
  149. package/es6/src/domain/prompt.js +0 -41
  150. package/es6/src/index.js +0 -9
  151. package/es6/src/models/compose.model.js +0 -254
  152. package/es6/src/models/config-yaml.model.js +0 -74
  153. package/es6/src/models/const.js +0 -20
  154. package/es6/src/models/dev-container.model.js +0 -162
  155. package/es6/src/models/docker-image.model.js +0 -235
  156. package/es6/src/models/env-file.model.js +0 -44
  157. package/es6/src/models/gitignore.model.js +0 -38
  158. package/es6/src/models/launch-json.model.js +0 -60
  159. package/es6/src/models/package-json.model.js +0 -141
  160. package/es6/src/models/project.model.js +0 -117
  161. package/es6/src/models/source-file.js +0 -1
  162. package/es6/src/models/tsconfig.model.js +0 -22
  163. package/es6/src/models/vitest-config.model.js +0 -23
  164. package/es6/src/models/vscode-settings.model.js +0 -19
  165. package/es6/src/program.js +0 -50
  166. package/es6/src/resolve-target-dir.js +0 -7
  167. package/es6/src/services/scaffold.service.js +0 -50
  168. package/es6/src/version.js +0 -34
  169. package/es6/src/webapi/api-name.js +0 -57
  170. package/es6/src/webapi/auth0-commands.js +0 -185
  171. package/es6/src/webapi/authorize-scopes.js +0 -55
  172. package/es6/src/webapi/cli-options.js +0 -34
  173. package/es6/src/webapi/const.js +0 -21
  174. package/es6/src/webapi/create-api.js +0 -245
  175. package/es6/src/webapi/devcontainer/codebase-rag-mcp.js +0 -154
  176. package/es6/src/webapi/devcontainer/codebase-rag-mcp.ts +0 -182
  177. package/es6/src/webapi/devcontainer/host-scripts.js +0 -29
  178. package/es6/src/webapi/devcontainer/templates/codebase-rag.env.example +0 -3
  179. package/es6/src/webapi/devcontainer/templates/initialize.sh +0 -169
  180. package/es6/src/webapi/devcontainer/templates/post-create.sh +0 -35
  181. package/es6/src/webapi/devcontainer/templates/post-start.sh +0 -65
  182. package/es6/src/webapi/devcontainer/templates/tty-prompt.sh +0 -38
  183. package/es6/src/webapi/features/auth.feature.js +0 -357
  184. package/es6/src/webapi/features/auth0-cli.js +0 -406
  185. package/es6/src/webapi/features/biome-format.js +0 -40
  186. package/es6/src/webapi/features/database.feature.js +0 -423
  187. package/es6/src/webapi/features/finalize.feature.js +0 -83
  188. package/es6/src/webapi/features/postman.feature.js +0 -256
  189. package/es6/src/webapi/models/api-package-json.model.js +0 -53
  190. package/es6/src/webapi/models/auth-config.model.js +0 -20
  191. package/es6/src/webapi/models/controller-source-file.model.js +0 -181
  192. package/es6/src/webapi/models/main-source-file.model.js +0 -161
  193. package/es6/src/webapi/models/production-dockerfile.model.js +0 -66
  194. package/es6/src/webapi/models/sample-files.model.js +0 -40
  195. package/es6/src/webapi/models/webapi-image.model.js +0 -18
  196. package/es6/src/webapi/postman/hello-openapi.js +0 -166
  197. package/es6/src/webapi/postman/local-view.js +0 -167
  198. package/es6/src/webapi/postman/openapi-to-postman.js +0 -179
  199. package/es6/src/webapi/workspace-readme.js +0 -88
  200. package/es6/src/webapi.action.js +0 -18
  201. package/es6/src/webapi.command.js +0 -33
  202. package/types/src/add-api.action.d.ts +0 -13
  203. package/types/src/add-api.command.d.ts +0 -7
  204. package/types/src/add.command.d.ts +0 -6
  205. package/types/src/add.module.d.ts +0 -14
  206. package/types/src/auth0-configure.action.d.ts +0 -7
  207. package/types/src/auth0-configure.command.d.ts +0 -6
  208. package/types/src/auth0-login.action.d.ts +0 -4
  209. package/types/src/auth0-login.command.d.ts +0 -6
  210. package/types/src/auth0.command.d.ts +0 -6
  211. package/types/src/auth0.module.d.ts +0 -18
  212. package/types/src/cli.d.ts +0 -2
  213. package/types/src/create.command.d.ts +0 -6
  214. package/types/src/create.module.d.ts +0 -14
  215. package/types/src/domain/prompt.d.ts +0 -4
  216. package/types/src/models/compose.model.d.ts +0 -73
  217. package/types/src/models/config-yaml.model.d.ts +0 -27
  218. package/types/src/models/const.d.ts +0 -20
  219. package/types/src/models/dev-container.model.d.ts +0 -49
  220. package/types/src/models/docker-image.model.d.ts +0 -90
  221. package/types/src/models/env-file.model.d.ts +0 -16
  222. package/types/src/models/gitignore.model.d.ts +0 -9
  223. package/types/src/models/launch-json.model.d.ts +0 -7
  224. package/types/src/models/package-json.model.d.ts +0 -36
  225. package/types/src/models/project.model.d.ts +0 -60
  226. package/types/src/models/tsconfig.model.d.ts +0 -5
  227. package/types/src/models/vitest-config.model.d.ts +0 -5
  228. package/types/src/models/vscode-settings.model.d.ts +0 -7
  229. package/types/src/program.d.ts +0 -8
  230. package/types/src/services/scaffold.service.d.ts +0 -22
  231. package/types/src/version.d.ts +0 -6
  232. package/types/src/webapi/api-name.d.ts +0 -14
  233. package/types/src/webapi/auth0-commands.d.ts +0 -11
  234. package/types/src/webapi/authorize-scopes.d.ts +0 -7
  235. package/types/src/webapi/cli-options.d.ts +0 -6
  236. package/types/src/webapi/create-api.d.ts +0 -15
  237. package/types/src/webapi/devcontainer/codebase-rag-mcp.d.ts +0 -32
  238. package/types/src/webapi/devcontainer/codebase-rag-mcp.ts +0 -182
  239. package/types/src/webapi/devcontainer/host-scripts.d.ts +0 -7
  240. package/types/src/webapi/devcontainer/templates/codebase-rag.env.example +0 -3
  241. package/types/src/webapi/devcontainer/templates/initialize.sh +0 -169
  242. package/types/src/webapi/devcontainer/templates/post-create.sh +0 -35
  243. package/types/src/webapi/devcontainer/templates/post-start.sh +0 -65
  244. package/types/src/webapi/devcontainer/templates/tty-prompt.sh +0 -38
  245. package/types/src/webapi/features/auth.feature.d.ts +0 -10
  246. package/types/src/webapi/features/auth0-cli.d.ts +0 -84
  247. package/types/src/webapi/features/biome-format.d.ts +0 -5
  248. package/types/src/webapi/features/database.feature.d.ts +0 -7
  249. package/types/src/webapi/features/finalize.feature.d.ts +0 -2
  250. package/types/src/webapi/features/postman.feature.d.ts +0 -16
  251. package/types/src/webapi/models/api-package-json.model.d.ts +0 -18
  252. package/types/src/webapi/models/auth-config.model.d.ts +0 -26
  253. package/types/src/webapi/models/controller-source-file.model.d.ts +0 -46
  254. package/types/src/webapi/models/github-ci.model.d.ts +0 -9
  255. package/types/src/webapi/models/main-source-file.model.d.ts +0 -54
  256. package/types/src/webapi/models/production-dockerfile.model.d.ts +0 -18
  257. package/types/src/webapi/models/sample-files.model.d.ts +0 -7
  258. package/types/src/webapi/models/webapi-image.model.d.ts +0 -12
  259. package/types/src/webapi/postman/hello-openapi.d.ts +0 -19
  260. package/types/src/webapi/postman/local-view.d.ts +0 -17
  261. package/types/src/webapi/postman/openapi-to-postman.d.ts +0 -62
  262. package/types/src/webapi/workspace-readme.d.ts +0 -18
  263. package/types/src/webapi.action.d.ts +0 -14
  264. package/types/src/webapi.command.d.ts +0 -11
@@ -0,0 +1,263 @@
1
+ import { readFileSync } from "node:fs";
2
+ import { join } from "node:path";
3
+ import { resetMemfs } from "@decohere/nexus/testing";
4
+ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
5
+ import { seedDevcontainerTemplates } from "./seed-devcontainer-templates.js";
6
+
7
+ /**
8
+ * Feature-level coverage for Auth0 + Postman wiring.
9
+ *
10
+ * webapi.spec.ts only asserts install behaviour with authProvider:"none" and
11
+ * stubs auth0-cli as isInstalled:false — so missing .env / empty auth0ClientId
12
+ * on re-scaffold never failed the suite.
13
+ */
14
+
15
+ const {
16
+ mockAsk,
17
+ mockPick,
18
+ mockInstall,
19
+ mockCreateAdminDatabaseUser,
20
+ mockEnsureSpaApp,
21
+ mockEnableConnectionForClient,
22
+ mockIsInstalled,
23
+ } = vi.hoisted(() => ({
24
+ mockAsk: vi.fn((_q: string, fallback = "") => Promise.resolve(fallback)),
25
+ mockPick: vi.fn(
26
+ <T>(_label: string, items: T[], _display: (item: T) => string): Promise<T | null> =>
27
+ Promise.resolve(items[0] ?? null),
28
+ ),
29
+ mockInstall: vi.fn().mockResolvedValue({ message: "", data: [] }),
30
+ mockCreateAdminDatabaseUser: vi.fn(),
31
+ mockEnsureSpaApp: vi.fn(),
32
+ mockEnableConnectionForClient: vi.fn(),
33
+ mockIsInstalled: vi.fn(() => true),
34
+ }));
35
+
36
+ const mockPm = {
37
+ name: "pnpm" as const,
38
+ install: mockInstall,
39
+ syncLockFile: vi.fn().mockResolvedValue({ message: "", data: [] }),
40
+ };
41
+
42
+ vi.mock("../src/domain/prompt.js", async () => {
43
+ const actual = await import("../src/domain/prompt.js");
44
+ return { ...actual, ask: mockAsk, pick: mockPick };
45
+ });
46
+
47
+ vi.mock(import("../../nexus/src/interop/index.js"), async (importOriginal) => {
48
+ const actual = await importOriginal();
49
+ return {
50
+ ...actual,
51
+ detectPackageManager: () => mockPm,
52
+ promptForPackageManager: () => Promise.resolve(mockPm),
53
+ };
54
+ });
55
+
56
+ vi.mock(import("../src/webapi/features/auth0-cli.js"), async (importOriginal) => {
57
+ const actual = await importOriginal();
58
+ return {
59
+ ...actual,
60
+ isInstalled: mockIsInstalled,
61
+ login: () => true,
62
+ listTenants: () => [{ name: "dev-tenant.us.auth0.com", active: true }],
63
+ listApis: () => [{ name: "webapi API", identifier: "https://api.webapi" }],
64
+ updateApiScopes: vi.fn(),
65
+ createAdminDatabaseUser: mockCreateAdminDatabaseUser,
66
+ ensureSpaApp: mockEnsureSpaApp,
67
+ createSpaApp: vi.fn(() => {
68
+ throw new Error("createSpaApp should not be called — use ensureSpaApp");
69
+ }),
70
+ enableConnectionForClient: mockEnableConnectionForClient,
71
+ generateAdminPassword: () => "Generated1!",
72
+ };
73
+ });
74
+
75
+ vi.mock("../src/webapi/features/biome-format.js", () => ({
76
+ formatScaffoldedFiles: vi.fn(),
77
+ }));
78
+
79
+ const { scaffoldApi } = await import("../src/webapi/create-api.js");
80
+
81
+ const TARGET = "/my-api";
82
+
83
+ /** Extra Auth0 fields accepted via the shared options bag (AuthFeature). */
84
+ type ScaffoldOpts = Parameters<typeof scaffoldApi>[0] & {
85
+ issuerBaseUrl?: string;
86
+ audience?: string;
87
+ adminUsername?: string;
88
+ adminPassword?: string;
89
+ };
90
+
91
+ function read(rel: string): string {
92
+ return readFileSync(join(TARGET, rel), "utf8");
93
+ }
94
+
95
+ const baseOpts = (): ScaffoldOpts => ({
96
+ d: TARGET,
97
+ databaseProvider: "postgres/typeorm",
98
+ authProvider: "auth0",
99
+ issuerBaseUrl: "https://dev-tenant.us.auth0.com",
100
+ audience: "https://api.webapi",
101
+ adminUsername: "admin",
102
+ adminPassword: "Generated1!",
103
+ postmanProvider: "local",
104
+ installDependencies: "no",
105
+ });
106
+
107
+ beforeEach(() => {
108
+ vi.spyOn(process, "cwd").mockReturnValue("/");
109
+ process.env.DECOHERE_BUILDER = "1";
110
+ resetMemfs();
111
+ seedDevcontainerTemplates();
112
+ mockInstall.mockClear();
113
+ mockAsk.mockClear();
114
+ mockCreateAdminDatabaseUser.mockReset();
115
+ mockEnsureSpaApp.mockReset();
116
+ mockEnableConnectionForClient.mockReset();
117
+
118
+ mockCreateAdminDatabaseUser.mockReturnValue({
119
+ user_id: "auth0|admin",
120
+ email: "admin@my-api.example.com",
121
+ password: "Generated1!",
122
+ username: "admin",
123
+ });
124
+ mockEnsureSpaApp.mockReturnValue({ client_id: "spa_client_123", reused: false });
125
+ });
126
+
127
+ afterEach(() => {
128
+ resetMemfs();
129
+ vi.restoreAllMocks();
130
+ delete process.env.DECOHERE_BUILDER;
131
+ });
132
+
133
+ describe("scaffoldApi auth0 + postman wiring", () => {
134
+ it("writes AUTH0_ADMIN_* to .env and auth0ClientId into the Postman environment", async () => {
135
+ await scaffoldApi(baseOpts());
136
+
137
+ const env = read(".env");
138
+ expect(env).toContain("AUTH0_ADMIN_USER_SUB=auth0|admin");
139
+ expect(env).toContain("AUTH0_ADMIN_USER_EMAIL=admin@my-api.example.com");
140
+ expect(env).toContain("AUTH0_ADMIN_USER_PASSWORD=Generated1!");
141
+ expect(env).toContain("AUTH0_ADMIN_USERNAME=admin");
142
+
143
+ const postmanEnv = read("postman/environments/my-api-Local.environment.yaml");
144
+ expect(postmanEnv).toMatch(/key: auth0ClientId\n\s+value: spa_client_123/);
145
+ expect(mockEnsureSpaApp).toHaveBeenCalledWith(
146
+ "my-api Postman",
147
+ expect.arrayContaining(["https://oauth.pstmn.io/v1/callback"]),
148
+ );
149
+ expect(mockCreateAdminDatabaseUser).toHaveBeenCalledOnce();
150
+
151
+ const readme = read("README.md");
152
+ expect(readme).toContain("## Auth0");
153
+ expect(readme).toContain("AUTH0_ISSUER_BASE_URL");
154
+ expect(readme).toContain("## Postman");
155
+ expect(readme).toContain("Local View");
156
+ });
157
+
158
+ it("still writes admin env when Auth0 returns an existing user identity", async () => {
159
+ mockCreateAdminDatabaseUser.mockReturnValue({
160
+ user_id: "auth0|existing",
161
+ email: "admin@my-api.example.com",
162
+ password: "Generated1!",
163
+ username: "admin",
164
+ });
165
+
166
+ await scaffoldApi(baseOpts());
167
+
168
+ const env = read(".env");
169
+ expect(env).toContain("AUTH0_ADMIN_USER_SUB=auth0|existing");
170
+ expect(env).toContain("AUTH0_ADMIN_USER_PASSWORD=Generated1!");
171
+ });
172
+
173
+ it("writes a non-empty auth0ClientId when the SPA app is reused", async () => {
174
+ mockEnsureSpaApp.mockReturnValue({ client_id: "spa_reused", reused: true });
175
+
176
+ await scaffoldApi(baseOpts());
177
+
178
+ const postmanEnv = read("postman/environments/my-api-Local.environment.yaml");
179
+ expect(postmanEnv).toMatch(/key: auth0ClientId\n\s+value: spa_reused/);
180
+ expect(postmanEnv).not.toMatch(/key: auth0ClientId\n\s+value: ""/);
181
+
182
+ const collectionAuth = read(
183
+ "postman/collections/my-api WebAPI/.resources/definition.yaml",
184
+ );
185
+ expect(collectionAuth).toMatch(/clientId:\s*spa_reused/);
186
+ });
187
+
188
+ it("preserves existing auth0ClientId when ensureSpaApp fails on re-scaffold", async () => {
189
+ await scaffoldApi(baseOpts());
190
+
191
+ mockEnsureSpaApp.mockImplementation(() => {
192
+ throw new Error("auth0 apps create failed: Unauthorized");
193
+ });
194
+
195
+ await scaffoldApi(baseOpts());
196
+
197
+ const postmanEnv = read("postman/environments/my-api-Local.environment.yaml");
198
+ expect(postmanEnv).toMatch(/key: auth0ClientId\n\s+value: spa_client_123/);
199
+ expect(postmanEnv).not.toMatch(/key: auth0ClientId\n\s+value: ""/);
200
+ });
201
+
202
+ it("fails the scaffold instead of writing an empty auth0ClientId", async () => {
203
+ mockEnsureSpaApp.mockImplementation(() => {
204
+ throw new Error("auth0 apps create failed: Unauthorized");
205
+ });
206
+
207
+ await expect(scaffoldApi(baseOpts())).rejects.toThrow(/auth0ClientId would be empty/);
208
+ });
209
+
210
+ it("surfaces Auth0 application-limit guidance without writing an empty auth0ClientId", async () => {
211
+ mockEnsureSpaApp.mockImplementation(() => {
212
+ throw new Error(
213
+ [
214
+ 'Auth0 refused to create SPA "my-api Postman": tenant application limit reached.',
215
+ "This tenant already has 3 application(s):",
216
+ " - ledger Postman (spa_a)",
217
+ "",
218
+ "Fix: Auth0 Dashboard → Applications → Applications → delete unused apps,",
219
+ 'then re-run the scaffold (or choose Postman "none" to skip SPA creation).',
220
+ ].join("\n"),
221
+ );
222
+ });
223
+
224
+ let thrown: unknown;
225
+ try {
226
+ await scaffoldApi(baseOpts());
227
+ } catch (err) {
228
+ thrown = err;
229
+ }
230
+ expect(thrown).toBeInstanceOf(Error);
231
+ const message = (thrown as Error).message;
232
+ expect(message).toMatch(/tenant application limit reached/);
233
+ expect(message).toMatch(/delete unused apps/i);
234
+ expect(message).not.toMatch(/auth0ClientId would be empty/);
235
+ });
236
+
237
+ it("scaffolds a thin ability.factory extension point (not CASL infrastructure)", async () => {
238
+ await scaffoldApi(baseOpts());
239
+
240
+ const ability = read("projects/api/src/auth/ability.factory.ts");
241
+ expect(ability).toContain("export function defineAbilitiesFor");
242
+ expect(ability).toContain("export function mapPrincipal");
243
+ expect(ability).toContain("defaultDefineAbilitiesFor");
244
+ expect(ability).toContain("createPrincipalFromPayload");
245
+ expect(ability).toContain("AUTH0_ADMIN_USER_SUB");
246
+ expect(ability).not.toContain("@casl/ability");
247
+ expect(ability).not.toContain("AbilityBuilder");
248
+ expect(ability).not.toContain("createMongoAbility");
249
+
250
+ const main = read("projects/api/src/main.ts");
251
+ expect(main).toContain(".withAuth(defineAbilitiesFor, { principalMapper: mapPrincipal })");
252
+ expect(main).not.toContain("createAbilityFactory");
253
+
254
+ const rootPkg = JSON.parse(read("package.json")) as {
255
+ dependencies?: Record<string, string>;
256
+ };
257
+ const apiPkg = JSON.parse(read("projects/api/package.json")) as {
258
+ dependencies?: Record<string, string>;
259
+ };
260
+ expect(rootPkg.dependencies?.["@casl/ability"]).toBeUndefined();
261
+ expect(apiPkg.dependencies?.["@casl/ability"]).toBeUndefined();
262
+ });
263
+ });
@@ -0,0 +1,232 @@
1
+ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
2
+
3
+ const { mockSpawnSync } = vi.hoisted(() => ({
4
+ mockSpawnSync: vi.fn(),
5
+ }));
6
+
7
+ vi.mock("node:child_process", () => ({
8
+ execFileSync: vi.fn(),
9
+ spawnSync: mockSpawnSync,
10
+ }));
11
+
12
+ const {
13
+ createAdminDatabaseUser,
14
+ createApi,
15
+ DB_CONNECTION_NAME,
16
+ ensureSpaApp,
17
+ updateApiScopes,
18
+ } = await import("../src/webapi/features/auth0-cli.js");
19
+
20
+ beforeEach(() => {
21
+ mockSpawnSync.mockReset();
22
+ });
23
+
24
+ afterEach(() => {
25
+ vi.clearAllMocks();
26
+ });
27
+
28
+ describe("createApi / updateApiScopes RBAC", () => {
29
+ it("creates APIs with --enforce-policies and access_token_authz dialect", () => {
30
+ mockSpawnSync.mockReturnValueOnce({
31
+ status: 0,
32
+ stdout: JSON.stringify({ identifier: "https://api.webapi" }),
33
+ stderr: "",
34
+ error: undefined,
35
+ });
36
+
37
+ const api = createApi("webapi API", "https://api.webapi");
38
+ expect(api.identifier).toBe("https://api.webapi");
39
+
40
+ const args = mockSpawnSync.mock.calls[0]?.[1] as string[];
41
+ expect(args).toEqual(
42
+ expect.arrayContaining([
43
+ "apis",
44
+ "create",
45
+ "--enforce-policies",
46
+ "--token-dialect",
47
+ "access_token_authz",
48
+ ]),
49
+ );
50
+ });
51
+
52
+ it("updates existing APIs with --enforce-policies so re-scaffold is not fail-open", () => {
53
+ mockSpawnSync.mockReturnValueOnce({
54
+ status: 0,
55
+ stdout: "",
56
+ stderr: "",
57
+ error: undefined,
58
+ });
59
+
60
+ updateApiScopes("https://api.webapi");
61
+
62
+ const args = mockSpawnSync.mock.calls[0]?.[1] as string[];
63
+ expect(args).toEqual(
64
+ expect.arrayContaining([
65
+ "apis",
66
+ "update",
67
+ "https://api.webapi",
68
+ "--enforce-policies",
69
+ "--token-dialect",
70
+ "access_token_authz",
71
+ ]),
72
+ );
73
+ });
74
+ });
75
+
76
+ describe("createAdminDatabaseUser", () => {
77
+ it("returns the created user on success", () => {
78
+ mockSpawnSync.mockReturnValueOnce({
79
+ status: 0,
80
+ stdout: JSON.stringify({ user_id: "auth0|new", email: "admin@demo.example.com" }),
81
+ stderr: "",
82
+ error: undefined,
83
+ });
84
+
85
+ const admin = createAdminDatabaseUser({
86
+ email: "admin@demo.example.com",
87
+ password: "Secret1!",
88
+ username: "admin",
89
+ });
90
+
91
+ expect(admin).toEqual({
92
+ user_id: "auth0|new",
93
+ email: "admin@demo.example.com",
94
+ password: "Secret1!",
95
+ username: "admin",
96
+ });
97
+ expect(mockSpawnSync).toHaveBeenCalledOnce();
98
+ });
99
+
100
+ it("looks up an existing user, resets password, and still returns credentials", () => {
101
+ mockSpawnSync
102
+ .mockReturnValueOnce({
103
+ status: 1,
104
+ stdout: "",
105
+ stderr: 'The user already exists. {"statusCode":409}',
106
+ error: undefined,
107
+ })
108
+ .mockReturnValueOnce({
109
+ status: 0,
110
+ stdout: JSON.stringify([
111
+ { user_id: "auth0|existing", email: "admin@demo.example.com" },
112
+ ]),
113
+ stderr: "",
114
+ error: undefined,
115
+ })
116
+ .mockReturnValueOnce({
117
+ status: 0,
118
+ stdout: "{}",
119
+ stderr: "",
120
+ error: undefined,
121
+ });
122
+
123
+ const admin = createAdminDatabaseUser({
124
+ email: "admin@demo.example.com",
125
+ password: "NewSecret1!",
126
+ username: "admin",
127
+ });
128
+
129
+ expect(admin).toEqual({
130
+ user_id: "auth0|existing",
131
+ email: "admin@demo.example.com",
132
+ password: "NewSecret1!",
133
+ username: "admin",
134
+ });
135
+
136
+ const patchCall = mockSpawnSync.mock.calls[2]?.[1] as string[];
137
+ expect(patchCall).toEqual(
138
+ expect.arrayContaining([
139
+ "api",
140
+ "patch",
141
+ `users/${encodeURIComponent("auth0|existing")}`,
142
+ ]),
143
+ );
144
+ const dataIdx = patchCall.indexOf("--data");
145
+ expect(JSON.parse(patchCall[dataIdx + 1] ?? "{}")).toEqual({
146
+ password: "NewSecret1!",
147
+ connection: DB_CONNECTION_NAME,
148
+ });
149
+ });
150
+ });
151
+
152
+ describe("ensureSpaApp", () => {
153
+ it("reuses an existing app by name and refreshes callbacks", () => {
154
+ mockSpawnSync
155
+ .mockReturnValueOnce({
156
+ status: 0,
157
+ stdout: JSON.stringify([
158
+ { client_id: "spa_existing", name: "webapi Postman" },
159
+ { client_id: "other", name: "Other App" },
160
+ ]),
161
+ stderr: "",
162
+ error: undefined,
163
+ })
164
+ .mockReturnValueOnce({
165
+ status: 0,
166
+ stdout: "",
167
+ stderr: "",
168
+ error: undefined,
169
+ });
170
+
171
+ const app = ensureSpaApp("webapi Postman", [
172
+ "https://oauth.pstmn.io/v1/callback",
173
+ "https://oauth.pstmn.io/v1/browser-callback",
174
+ ]);
175
+
176
+ expect(app).toEqual({ client_id: "spa_existing", reused: true });
177
+ const updateArgs = mockSpawnSync.mock.calls[1]?.[1] as string[];
178
+ expect(updateArgs).toEqual(
179
+ expect.arrayContaining([
180
+ "apps",
181
+ "update",
182
+ "spa_existing",
183
+ "--callbacks",
184
+ "https://oauth.pstmn.io/v1/callback,https://oauth.pstmn.io/v1/browser-callback",
185
+ ]),
186
+ );
187
+ });
188
+
189
+ it("creates a SPA when no matching app exists", () => {
190
+ mockSpawnSync
191
+ .mockReturnValueOnce({
192
+ status: 0,
193
+ stdout: JSON.stringify([{ client_id: "other", name: "Other App" }]),
194
+ stderr: "",
195
+ error: undefined,
196
+ })
197
+ .mockReturnValueOnce({
198
+ status: 0,
199
+ stdout: JSON.stringify({ client_id: "spa_new", name: "webapi Postman" }),
200
+ stderr: "",
201
+ error: undefined,
202
+ });
203
+
204
+ const app = ensureSpaApp("webapi Postman", ["https://oauth.pstmn.io/v1/callback"]);
205
+ expect(app).toEqual({ client_id: "spa_new", reused: false });
206
+ });
207
+
208
+ it("fails with a delete-apps message when Auth0 hits the application entity limit", () => {
209
+ mockSpawnSync
210
+ .mockReturnValueOnce({
211
+ status: 0,
212
+ stdout: JSON.stringify([
213
+ { client_id: "spa_a", name: "ledger Postman" },
214
+ { client_id: "spa_b", name: "workspace Postman" },
215
+ { client_id: "m2m", name: "Default App" },
216
+ ]),
217
+ stderr: "",
218
+ error: undefined,
219
+ })
220
+ .mockReturnValueOnce({
221
+ status: 1,
222
+ stdout: "",
223
+ stderr:
224
+ "Failed to create application: 403 Forbidden: You reached the limit of entities of this type for this tenant.",
225
+ error: undefined,
226
+ });
227
+
228
+ expect(() =>
229
+ ensureSpaApp("crypto Postman", ["https://oauth.pstmn.io/v1/callback"]),
230
+ ).toThrow(/tenant application limit reached[\s\S]*delete unused apps/i);
231
+ });
232
+ });
@@ -0,0 +1,49 @@
1
+ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
2
+
3
+ const { mockSpawnSync } = vi.hoisted(() => ({
4
+ mockSpawnSync: vi.fn(),
5
+ }));
6
+
7
+ vi.mock("node:child_process", () => ({
8
+ spawnSync: mockSpawnSync,
9
+ }));
10
+
11
+ const { formatScaffoldedFiles } = await import("../src/webapi/features/biome-format.js");
12
+
13
+ beforeEach(() => {
14
+ mockSpawnSync.mockReset();
15
+ });
16
+
17
+ afterEach(() => {
18
+ vi.clearAllMocks();
19
+ });
20
+
21
+ describe("formatScaffoldedFiles", () => {
22
+ it("runs biome check --write in the target directory", () => {
23
+ mockSpawnSync.mockReturnValue({ status: 0, stdout: "", stderr: "", error: undefined });
24
+
25
+ formatScaffoldedFiles("/tmp/scaffold");
26
+
27
+ expect(mockSpawnSync).toHaveBeenCalledWith(
28
+ "biome",
29
+ ["check", "--write", "."],
30
+ expect.objectContaining({ cwd: "/tmp/scaffold" }),
31
+ );
32
+ });
33
+
34
+ it("falls back to npx when biome is not on PATH", () => {
35
+ const enoent = Object.assign(new Error("not found"), { code: "ENOENT" });
36
+ mockSpawnSync
37
+ .mockReturnValueOnce({ status: 1, stdout: "", stderr: "", error: enoent })
38
+ .mockReturnValueOnce({ status: 0, stdout: "", stderr: "", error: undefined });
39
+
40
+ formatScaffoldedFiles("/tmp/scaffold");
41
+
42
+ expect(mockSpawnSync).toHaveBeenNthCalledWith(
43
+ 2,
44
+ "npx",
45
+ ["--yes", "@biomejs/biome", "check", "--write", "."],
46
+ expect.objectContaining({ cwd: "/tmp/scaffold" }),
47
+ );
48
+ });
49
+ });