@prenta/cli 0.14.0

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 (207) hide show
  1. package/.turbo/turbo-build.log +4 -0
  2. package/.turbo/turbo-test.log +353 -0
  3. package/CHANGELOG.md +404 -0
  4. package/LICENSE +21 -0
  5. package/README.md +30 -0
  6. package/dist/__tests__/db-init.test.d.ts +2 -0
  7. package/dist/__tests__/db-init.test.d.ts.map +1 -0
  8. package/dist/__tests__/db-init.test.js +127 -0
  9. package/dist/__tests__/db-init.test.js.map +1 -0
  10. package/dist/__tests__/db-sync-resolver.test.d.ts +2 -0
  11. package/dist/__tests__/db-sync-resolver.test.d.ts.map +1 -0
  12. package/dist/__tests__/db-sync-resolver.test.js +25 -0
  13. package/dist/__tests__/db-sync-resolver.test.js.map +1 -0
  14. package/dist/__tests__/db-sync.test.d.ts +2 -0
  15. package/dist/__tests__/db-sync.test.d.ts.map +1 -0
  16. package/dist/__tests__/db-sync.test.js +167 -0
  17. package/dist/__tests__/db-sync.test.js.map +1 -0
  18. package/dist/__tests__/deployment-diagnostics.test.d.ts +2 -0
  19. package/dist/__tests__/deployment-diagnostics.test.d.ts.map +1 -0
  20. package/dist/__tests__/deployment-diagnostics.test.js +401 -0
  21. package/dist/__tests__/deployment-diagnostics.test.js.map +1 -0
  22. package/dist/__tests__/form-seed.test.d.ts +2 -0
  23. package/dist/__tests__/form-seed.test.d.ts.map +1 -0
  24. package/dist/__tests__/form-seed.test.js +79 -0
  25. package/dist/__tests__/form-seed.test.js.map +1 -0
  26. package/dist/__tests__/init.test.d.ts +2 -0
  27. package/dist/__tests__/init.test.d.ts.map +1 -0
  28. package/dist/__tests__/init.test.js +25 -0
  29. package/dist/__tests__/init.test.js.map +1 -0
  30. package/dist/__tests__/load-dotenv.test.d.ts +2 -0
  31. package/dist/__tests__/load-dotenv.test.d.ts.map +1 -0
  32. package/dist/__tests__/load-dotenv.test.js +40 -0
  33. package/dist/__tests__/load-dotenv.test.js.map +1 -0
  34. package/dist/__tests__/mcp-init.test.d.ts +2 -0
  35. package/dist/__tests__/mcp-init.test.d.ts.map +1 -0
  36. package/dist/__tests__/mcp-init.test.js +100 -0
  37. package/dist/__tests__/mcp-init.test.js.map +1 -0
  38. package/dist/__tests__/prisma-client-path.test.d.ts +2 -0
  39. package/dist/__tests__/prisma-client-path.test.d.ts.map +1 -0
  40. package/dist/__tests__/prisma-client-path.test.js +66 -0
  41. package/dist/__tests__/prisma-client-path.test.js.map +1 -0
  42. package/dist/__tests__/schema-fragment.test.d.ts +2 -0
  43. package/dist/__tests__/schema-fragment.test.d.ts.map +1 -0
  44. package/dist/__tests__/schema-fragment.test.js +40 -0
  45. package/dist/__tests__/schema-fragment.test.js.map +1 -0
  46. package/dist/__tests__/seed.test.d.ts +2 -0
  47. package/dist/__tests__/seed.test.d.ts.map +1 -0
  48. package/dist/__tests__/seed.test.js +207 -0
  49. package/dist/__tests__/seed.test.js.map +1 -0
  50. package/dist/__tests__/upgrade.test.d.ts +2 -0
  51. package/dist/__tests__/upgrade.test.d.ts.map +1 -0
  52. package/dist/__tests__/upgrade.test.js +41 -0
  53. package/dist/__tests__/upgrade.test.js.map +1 -0
  54. package/dist/__tests__/vercel-env-matrix.test.d.ts +2 -0
  55. package/dist/__tests__/vercel-env-matrix.test.d.ts.map +1 -0
  56. package/dist/__tests__/vercel-env-matrix.test.js +56 -0
  57. package/dist/__tests__/vercel-env-matrix.test.js.map +1 -0
  58. package/dist/commands/db-init.d.ts +24 -0
  59. package/dist/commands/db-init.d.ts.map +1 -0
  60. package/dist/commands/db-init.js +195 -0
  61. package/dist/commands/db-init.js.map +1 -0
  62. package/dist/commands/db-status.d.ts +3 -0
  63. package/dist/commands/db-status.d.ts.map +1 -0
  64. package/dist/commands/db-status.js +108 -0
  65. package/dist/commands/db-status.js.map +1 -0
  66. package/dist/commands/db-sync.d.ts +66 -0
  67. package/dist/commands/db-sync.d.ts.map +1 -0
  68. package/dist/commands/db-sync.js +310 -0
  69. package/dist/commands/db-sync.js.map +1 -0
  70. package/dist/commands/doctor.d.ts +5 -0
  71. package/dist/commands/doctor.d.ts.map +1 -0
  72. package/dist/commands/doctor.js +282 -0
  73. package/dist/commands/doctor.js.map +1 -0
  74. package/dist/commands/export.d.ts +3 -0
  75. package/dist/commands/export.d.ts.map +1 -0
  76. package/dist/commands/export.js +99 -0
  77. package/dist/commands/export.js.map +1 -0
  78. package/dist/commands/generate.d.ts +3 -0
  79. package/dist/commands/generate.d.ts.map +1 -0
  80. package/dist/commands/generate.js +24 -0
  81. package/dist/commands/generate.js.map +1 -0
  82. package/dist/commands/import.d.ts +3 -0
  83. package/dist/commands/import.d.ts.map +1 -0
  84. package/dist/commands/import.js +201 -0
  85. package/dist/commands/import.js.map +1 -0
  86. package/dist/commands/init.d.ts +4 -0
  87. package/dist/commands/init.d.ts.map +1 -0
  88. package/dist/commands/init.js +31 -0
  89. package/dist/commands/init.js.map +1 -0
  90. package/dist/commands/mcp.d.ts +3 -0
  91. package/dist/commands/mcp.d.ts.map +1 -0
  92. package/dist/commands/mcp.js +126 -0
  93. package/dist/commands/mcp.js.map +1 -0
  94. package/dist/commands/migrate-media-usage.d.ts +3 -0
  95. package/dist/commands/migrate-media-usage.d.ts.map +1 -0
  96. package/dist/commands/migrate-media-usage.js +52 -0
  97. package/dist/commands/migrate-media-usage.js.map +1 -0
  98. package/dist/commands/migrate-meta.d.ts +3 -0
  99. package/dist/commands/migrate-meta.d.ts.map +1 -0
  100. package/dist/commands/migrate-meta.js +81 -0
  101. package/dist/commands/migrate-meta.js.map +1 -0
  102. package/dist/commands/migrate-schema.d.ts +3 -0
  103. package/dist/commands/migrate-schema.d.ts.map +1 -0
  104. package/dist/commands/migrate-schema.js +84 -0
  105. package/dist/commands/migrate-schema.js.map +1 -0
  106. package/dist/commands/migrate-sections.d.ts +3 -0
  107. package/dist/commands/migrate-sections.d.ts.map +1 -0
  108. package/dist/commands/migrate-sections.js +56 -0
  109. package/dist/commands/migrate-sections.js.map +1 -0
  110. package/dist/commands/migrate.d.ts +3 -0
  111. package/dist/commands/migrate.d.ts.map +1 -0
  112. package/dist/commands/migrate.js +51 -0
  113. package/dist/commands/migrate.js.map +1 -0
  114. package/dist/commands/seed.d.ts +60 -0
  115. package/dist/commands/seed.d.ts.map +1 -0
  116. package/dist/commands/seed.js +585 -0
  117. package/dist/commands/seed.js.map +1 -0
  118. package/dist/commands/update-check.d.ts +3 -0
  119. package/dist/commands/update-check.d.ts.map +1 -0
  120. package/dist/commands/update-check.js +132 -0
  121. package/dist/commands/update-check.js.map +1 -0
  122. package/dist/commands/upgrade.d.ts +4 -0
  123. package/dist/commands/upgrade.d.ts.map +1 -0
  124. package/dist/commands/upgrade.js +175 -0
  125. package/dist/commands/upgrade.js.map +1 -0
  126. package/dist/commands/vercel-blob-link.d.ts +3 -0
  127. package/dist/commands/vercel-blob-link.d.ts.map +1 -0
  128. package/dist/commands/vercel-blob-link.js +83 -0
  129. package/dist/commands/vercel-blob-link.js.map +1 -0
  130. package/dist/deployment/diagnostics.d.ts +136 -0
  131. package/dist/deployment/diagnostics.d.ts.map +1 -0
  132. package/dist/deployment/diagnostics.js +575 -0
  133. package/dist/deployment/diagnostics.js.map +1 -0
  134. package/dist/index.d.ts +3 -0
  135. package/dist/index.d.ts.map +1 -0
  136. package/dist/index.js +47 -0
  137. package/dist/index.js.map +1 -0
  138. package/dist/utils/database.d.ts +21 -0
  139. package/dist/utils/database.d.ts.map +1 -0
  140. package/dist/utils/database.js +98 -0
  141. package/dist/utils/database.js.map +1 -0
  142. package/dist/utils/form-seed.d.ts +15 -0
  143. package/dist/utils/form-seed.d.ts.map +1 -0
  144. package/dist/utils/form-seed.js +97 -0
  145. package/dist/utils/form-seed.js.map +1 -0
  146. package/dist/utils/load-dotenv.d.ts +11 -0
  147. package/dist/utils/load-dotenv.d.ts.map +1 -0
  148. package/dist/utils/load-dotenv.js +47 -0
  149. package/dist/utils/load-dotenv.js.map +1 -0
  150. package/dist/utils/logger.d.ts +8 -0
  151. package/dist/utils/logger.d.ts.map +1 -0
  152. package/dist/utils/logger.js +16 -0
  153. package/dist/utils/logger.js.map +1 -0
  154. package/dist/utils/prisma-client-path.d.ts +20 -0
  155. package/dist/utils/prisma-client-path.d.ts.map +1 -0
  156. package/dist/utils/prisma-client-path.js +77 -0
  157. package/dist/utils/prisma-client-path.js.map +1 -0
  158. package/dist/vercel/client.d.ts +32 -0
  159. package/dist/vercel/client.d.ts.map +1 -0
  160. package/dist/vercel/client.js +74 -0
  161. package/dist/vercel/client.js.map +1 -0
  162. package/dist/vercel/env-matrix.d.ts +35 -0
  163. package/dist/vercel/env-matrix.d.ts.map +1 -0
  164. package/dist/vercel/env-matrix.js +66 -0
  165. package/dist/vercel/env-matrix.js.map +1 -0
  166. package/package.json +41 -0
  167. package/src/__tests__/db-init.test.ts +155 -0
  168. package/src/__tests__/db-sync-resolver.test.ts +29 -0
  169. package/src/__tests__/db-sync.test.ts +221 -0
  170. package/src/__tests__/deployment-diagnostics.test.ts +474 -0
  171. package/src/__tests__/form-seed.test.ts +91 -0
  172. package/src/__tests__/init.test.ts +30 -0
  173. package/src/__tests__/load-dotenv.test.ts +53 -0
  174. package/src/__tests__/mcp-init.test.ts +125 -0
  175. package/src/__tests__/prisma-client-path.test.ts +87 -0
  176. package/src/__tests__/schema-fragment.test.ts +51 -0
  177. package/src/__tests__/seed.test.ts +249 -0
  178. package/src/__tests__/upgrade.test.ts +44 -0
  179. package/src/__tests__/vercel-env-matrix.test.ts +65 -0
  180. package/src/commands/db-init.ts +211 -0
  181. package/src/commands/db-status.ts +122 -0
  182. package/src/commands/db-sync.ts +367 -0
  183. package/src/commands/doctor.ts +354 -0
  184. package/src/commands/export.ts +118 -0
  185. package/src/commands/generate.ts +26 -0
  186. package/src/commands/import.ts +228 -0
  187. package/src/commands/init.ts +36 -0
  188. package/src/commands/mcp.ts +181 -0
  189. package/src/commands/migrate-media-usage.ts +68 -0
  190. package/src/commands/migrate-meta.ts +107 -0
  191. package/src/commands/migrate-schema.ts +110 -0
  192. package/src/commands/migrate-sections.ts +73 -0
  193. package/src/commands/migrate.ts +56 -0
  194. package/src/commands/seed.ts +693 -0
  195. package/src/commands/update-check.ts +152 -0
  196. package/src/commands/upgrade.ts +202 -0
  197. package/src/commands/vercel-blob-link.ts +116 -0
  198. package/src/deployment/diagnostics.ts +735 -0
  199. package/src/index.ts +54 -0
  200. package/src/utils/database.ts +126 -0
  201. package/src/utils/form-seed.ts +137 -0
  202. package/src/utils/load-dotenv.ts +52 -0
  203. package/src/utils/logger.ts +26 -0
  204. package/src/utils/prisma-client-path.ts +81 -0
  205. package/src/vercel/client.ts +112 -0
  206. package/src/vercel/env-matrix.ts +112 -0
  207. package/tsconfig.json +9 -0
@@ -0,0 +1,125 @@
1
+ import { mkdtemp, mkdir, readFile, rm, writeFile } from 'node:fs/promises'
2
+ import { tmpdir } from 'node:os'
3
+ import path from 'node:path'
4
+ import { Command } from 'commander'
5
+ import { afterEach, beforeEach, describe, expect, it } from 'vitest'
6
+
7
+ import { registerMcpCommand } from '../commands/mcp.js'
8
+
9
+ /**
10
+ * `prenta mcp:init` retrofits MCP editor configs onto existing projects.
11
+ * Pins the merge contract: other configured servers are never clobbered, an
12
+ * existing `prenta` entry needs `--force`, and both config paths end up
13
+ * git-ignored (they hold a real API key once configured).
14
+ */
15
+
16
+ let projectDir: string
17
+ let originalCwd: string
18
+
19
+ async function run(args: string[] = []): Promise<void> {
20
+ const program = new Command()
21
+ program.exitOverride()
22
+ registerMcpCommand(program)
23
+ await program.parseAsync(['mcp:init', ...args], { from: 'user' })
24
+ }
25
+
26
+ async function readJson(relPath: string): Promise<any> {
27
+ return JSON.parse(await readFile(path.join(projectDir, relPath), 'utf8'))
28
+ }
29
+
30
+ beforeEach(async () => {
31
+ originalCwd = process.cwd()
32
+ projectDir = await mkdtemp(path.join(tmpdir(), 'prenta-mcp-init-'))
33
+ process.chdir(projectDir)
34
+ })
35
+
36
+ afterEach(async () => {
37
+ process.chdir(originalCwd)
38
+ process.exitCode = 0
39
+ await rm(projectDir, { recursive: true, force: true })
40
+ })
41
+
42
+ describe('mcp:init', () => {
43
+ it('creates both editor configs with placeholder key and git-ignores them', async () => {
44
+ await writeFile(path.join(projectDir, '.gitignore'), 'node_modules\n', 'utf8')
45
+ await run()
46
+
47
+ const cursor = await readJson('.cursor/mcp.json')
48
+ expect(cursor.mcpServers.prenta.command).toBe('npx')
49
+ expect(cursor.mcpServers.prenta.args).toEqual(['-y', '@prenta/mcp-server'])
50
+ expect(cursor.mcpServers.prenta.env.PRENTA_BASE_URL).toBe('http://localhost:3000')
51
+ expect(cursor.mcpServers.prenta.env.PRENTA_API_KEY).toBe('act_sk_REPLACE_WITH_YOUR_KEY')
52
+
53
+ const vscode = await readJson('.vscode/mcp.json')
54
+ expect(vscode.servers.prenta.type).toBe('stdio')
55
+
56
+ const gitignore = await readFile(path.join(projectDir, '.gitignore'), 'utf8')
57
+ expect(gitignore).toContain('node_modules')
58
+ expect(gitignore).toContain('.cursor/mcp.json')
59
+ expect(gitignore).toContain('.vscode/mcp.json')
60
+ })
61
+
62
+ it('applies --url and --key to both configs', async () => {
63
+ await run(['--url', 'https://example.com', '--key', 'act_sk_real123'])
64
+
65
+ const cursor = await readJson('.cursor/mcp.json')
66
+ expect(cursor.mcpServers.prenta.env).toEqual({
67
+ PRENTA_BASE_URL: 'https://example.com',
68
+ PRENTA_API_KEY: 'act_sk_real123',
69
+ })
70
+ const vscode = await readJson('.vscode/mcp.json')
71
+ expect(vscode.servers.prenta.env.PRENTA_BASE_URL).toBe('https://example.com')
72
+ })
73
+
74
+ it('merges into an existing config without clobbering other servers', async () => {
75
+ await mkdir(path.join(projectDir, '.cursor'), { recursive: true })
76
+ await writeFile(
77
+ path.join(projectDir, '.cursor', 'mcp.json'),
78
+ JSON.stringify({ mcpServers: { sentry: { command: 'sentry-mcp' } } }),
79
+ 'utf8',
80
+ )
81
+ await run()
82
+
83
+ const cursor = await readJson('.cursor/mcp.json')
84
+ expect(cursor.mcpServers.sentry).toEqual({ command: 'sentry-mcp' })
85
+ expect(cursor.mcpServers.prenta).toBeDefined()
86
+ })
87
+
88
+ it('leaves an existing prenta entry alone unless --force is passed', async () => {
89
+ await mkdir(path.join(projectDir, '.cursor'), { recursive: true })
90
+ const existing = {
91
+ mcpServers: { prenta: { command: 'custom', env: { PRENTA_BASE_URL: 'https://keep.me' } } },
92
+ }
93
+ await writeFile(path.join(projectDir, '.cursor', 'mcp.json'), JSON.stringify(existing), 'utf8')
94
+
95
+ await run()
96
+ let cursor = await readJson('.cursor/mcp.json')
97
+ expect(cursor.mcpServers.prenta.command).toBe('custom')
98
+
99
+ await run(['--force', '--url', 'https://new.example.com'])
100
+ cursor = await readJson('.cursor/mcp.json')
101
+ expect(cursor.mcpServers.prenta.command).toBe('npx')
102
+ expect(cursor.mcpServers.prenta.env.PRENTA_BASE_URL).toBe('https://new.example.com')
103
+ })
104
+
105
+ it('fails on invalid JSON without --force, overwrites with --force', async () => {
106
+ await mkdir(path.join(projectDir, '.cursor'), { recursive: true })
107
+ await writeFile(path.join(projectDir, '.cursor', 'mcp.json'), '{ not json', 'utf8')
108
+
109
+ await run()
110
+ expect(process.exitCode).toBe(1)
111
+ process.exitCode = 0
112
+
113
+ await run(['--force'])
114
+ const cursor = await readJson('.cursor/mcp.json')
115
+ expect(cursor.mcpServers.prenta).toBeDefined()
116
+ })
117
+
118
+ it('does not duplicate gitignore entries on re-runs', async () => {
119
+ await run()
120
+ await run(['--force'])
121
+ const gitignore = await readFile(path.join(projectDir, '.gitignore'), 'utf8')
122
+ const count = gitignore.split('\n').filter((l) => l.trim() === '.cursor/mcp.json').length
123
+ expect(count).toBe(1)
124
+ })
125
+ })
@@ -0,0 +1,87 @@
1
+ import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'
2
+ import { tmpdir } from 'node:os'
3
+ import path from 'node:path'
4
+ import { afterEach, describe, expect, it } from 'vitest'
5
+
6
+ import {
7
+ findPrismaSchemaFile,
8
+ parsePrismaGeneratorOutputDir,
9
+ resolveClientEntryInDir,
10
+ resolveProjectPrismaClientModule,
11
+ } from '../utils/prisma-client-path.js'
12
+
13
+ describe('prisma-client-path', () => {
14
+ let tempDir: string | null = null
15
+
16
+ afterEach(() => {
17
+ if (tempDir) rmSync(tempDir, { recursive: true, force: true })
18
+ tempDir = null
19
+ })
20
+
21
+ function writeSchema(root: string, output: string) {
22
+ mkdirSync(path.join(root, 'prisma'), { recursive: true })
23
+ writeFileSync(
24
+ path.join(root, 'prisma', 'schema.prisma'),
25
+ `generator client {
26
+ provider = "prisma-client"
27
+ output = "${output}"
28
+ }
29
+
30
+ datasource db {
31
+ provider = "postgresql"
32
+ }
33
+ `,
34
+ )
35
+ }
36
+
37
+ function writeClient(clientPath: string) {
38
+ mkdirSync(path.dirname(clientPath), { recursive: true })
39
+ writeFileSync(clientPath, 'export class PrismaClient {}')
40
+ }
41
+
42
+ it('finds prisma/schema.prisma in the project root', () => {
43
+ tempDir = mkdtempSync(path.join(tmpdir(), 'prenta-cli-prisma-'))
44
+ writeSchema(tempDir, '../generated/prisma')
45
+ expect(findPrismaSchemaFile(tempDir)).toBe(path.join(tempDir, 'prisma', 'schema.prisma'))
46
+ })
47
+
48
+ it('resolves relative generator output from the schema directory', () => {
49
+ tempDir = mkdtempSync(path.join(tmpdir(), 'prenta-cli-prisma-'))
50
+ writeSchema(tempDir, '../generated/prisma')
51
+ const schemaPath = path.join(tempDir, 'prisma', 'schema.prisma')
52
+ expect(parsePrismaGeneratorOutputDir(schemaPath)).toBe(
53
+ path.join(tempDir, 'generated', 'prisma'),
54
+ )
55
+ })
56
+
57
+ it('resolves custom output paths used by Prisma 7 scaffolds', () => {
58
+ tempDir = mkdtempSync(path.join(tmpdir(), 'prenta-cli-prisma-'))
59
+ writeSchema(tempDir, '../src/generated/prisma')
60
+ const schemaPath = path.join(tempDir, 'prisma', 'schema.prisma')
61
+ expect(parsePrismaGeneratorOutputDir(schemaPath)).toBe(
62
+ path.join(tempDir, 'src', 'generated', 'prisma'),
63
+ )
64
+ })
65
+
66
+ it('locates client.ts via schema output before default fallbacks', () => {
67
+ tempDir = mkdtempSync(path.join(tmpdir(), 'prenta-cli-prisma-'))
68
+ writeSchema(tempDir, '../src/generated/prisma')
69
+ const clientPath = path.join(tempDir, 'src', 'generated', 'prisma', 'client.ts')
70
+ writeClient(clientPath)
71
+ expect(resolveProjectPrismaClientModule(tempDir)).toBe(clientPath)
72
+ })
73
+
74
+ it('falls back to generated/prisma/client.ts when schema output is missing', () => {
75
+ tempDir = mkdtempSync(path.join(tmpdir(), 'prenta-cli-prisma-'))
76
+ const clientPath = path.join(tempDir, 'generated', 'prisma', 'client.ts')
77
+ writeClient(clientPath)
78
+ expect(resolveProjectPrismaClientModule(tempDir)).toBe(clientPath)
79
+ })
80
+
81
+ it('resolveClientEntryInDir prefers client.ts over index.ts', () => {
82
+ tempDir = mkdtempSync(path.join(tmpdir(), 'prenta-cli-prisma-entry-'))
83
+ writeFileSync(path.join(tempDir, 'index.ts'), 'export {}')
84
+ writeFileSync(path.join(tempDir, 'client.ts'), 'export class PrismaClient {}')
85
+ expect(resolveClientEntryInDir(tempDir)).toBe(path.join(tempDir, 'client.ts'))
86
+ })
87
+ })
@@ -0,0 +1,51 @@
1
+ import { Command } from 'commander'
2
+ import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
3
+ import { tmpdir } from 'node:os'
4
+ import path from 'node:path'
5
+ import { afterEach, describe, expect, it, vi } from 'vitest'
6
+
7
+ import {
8
+ registerDbInitCommand,
9
+ resetDbInitCommandRunner,
10
+ setDbInitCommandRunner,
11
+ } from '../commands/db-init.js'
12
+ import { REQUIRED_CMS_MODELS } from '../deployment/diagnostics.js'
13
+
14
+ function baseSchema() {
15
+ return `generator client {
16
+ provider = "prisma-client-js"
17
+ }
18
+
19
+ datasource db {
20
+ provider = "postgresql"
21
+ url = env("DATABASE_URL")
22
+ }
23
+ `
24
+ }
25
+
26
+ describe('db:init schema fragment', () => {
27
+ afterEach(() => {
28
+ resetDbInitCommandRunner()
29
+ vi.restoreAllMocks()
30
+ })
31
+
32
+ it('injects all deploy-critical Prenta models', async () => {
33
+ const tempRoot = await mkdtemp(path.join(tmpdir(), 'prenta-db-init-'))
34
+ const schemaPath = path.join(tempRoot, 'schema.prisma')
35
+ await writeFile(schemaPath, baseSchema())
36
+
37
+ vi.spyOn(process, 'cwd').mockReturnValue(tempRoot)
38
+ setDbInitCommandRunner(() => undefined)
39
+ const command = new Command()
40
+ registerDbInitCommand(command)
41
+
42
+ await command.parseAsync(['node', 'prenta', 'db:init', '--schema', 'schema.prisma'])
43
+
44
+ const schema = await readFile(schemaPath, 'utf-8')
45
+ for (const model of REQUIRED_CMS_MODELS) {
46
+ expect(schema).toContain(`model ${model} `)
47
+ }
48
+
49
+ await rm(tempRoot, { recursive: true, force: true })
50
+ })
51
+ })
@@ -0,0 +1,249 @@
1
+ import { afterEach, describe, expect, it, vi } from 'vitest'
2
+
3
+ vi.mock('@prenta/core', () => ({
4
+ extractPlainText: vi.fn((value: string) => value.replace(/<[^>]+>/g, ' ')),
5
+ hashContent: vi.fn(async () => 'hash-1'),
6
+ sanitizeHtml: vi.fn((value: string) => value.replace(/<script[\s\S]*?<\/script>/gi, '')),
7
+ createInitialAdmin: vi.fn(),
8
+ }))
9
+
10
+ import {
11
+ createSeedDocument,
12
+ ensureSeedAdmin,
13
+ normalizeSeedPayload,
14
+ DEMO_NAVIGATIONS,
15
+ } from '../commands/seed.js'
16
+
17
+ afterEach(() => {
18
+ vi.unstubAllEnvs()
19
+ })
20
+
21
+ describe('seed payload normalization', () => {
22
+ it('separates globals from collection documents', () => {
23
+ const normalized = normalizeSeedPayload({
24
+ globals: {
25
+ 'site-settings': {
26
+ siteName: 'MaidPro',
27
+ phone: '555-0100',
28
+ },
29
+ },
30
+ collections: {
31
+ pages: [
32
+ {
33
+ data: { title: 'Home', slug: 'home' },
34
+ status: 'PUBLISHED',
35
+ },
36
+ ],
37
+ },
38
+ })
39
+
40
+ expect(normalized.globals).toEqual([
41
+ {
42
+ slug: 'site-settings',
43
+ data: { siteName: 'MaidPro', phone: '555-0100' },
44
+ },
45
+ ])
46
+ expect(normalized.documents).toEqual([
47
+ {
48
+ collection: 'pages',
49
+ data: { title: 'Home', slug: 'home' },
50
+ status: 'PUBLISHED',
51
+ },
52
+ ])
53
+ })
54
+
55
+ it('keeps backwards-compatible collection-key seed files', () => {
56
+ const normalized = normalizeSeedPayload({
57
+ pages: [{ title: 'Home', slug: 'home' }],
58
+ })
59
+
60
+ expect(normalized.globals).toEqual([])
61
+ expect(normalized.documents).toEqual([
62
+ {
63
+ collection: 'pages',
64
+ data: { title: 'Home', slug: 'home' },
65
+ status: 'DRAFT',
66
+ },
67
+ ])
68
+ })
69
+ })
70
+
71
+ describe('seed admin handling', () => {
72
+ it('refuses to create a first admin without credentials', async () => {
73
+ const db = {
74
+ user: {
75
+ findFirst: vi.fn().mockResolvedValue(null),
76
+ },
77
+ }
78
+
79
+ await expect(ensureSeedAdmin(db)).rejects.toThrow('CMS_ADMIN_EMAIL and CMS_ADMIN_PASSWORD')
80
+ })
81
+ })
82
+
83
+ describe('seed document creation', () => {
84
+ it('creates documents with versions, content hashes, and sanitized HTML', async () => {
85
+ const tx = {
86
+ document: {
87
+ create: vi.fn().mockResolvedValue({ id: 'doc-1' }),
88
+ },
89
+ version: {
90
+ create: vi.fn(),
91
+ },
92
+ }
93
+ const db = {
94
+ $transaction: vi.fn(async (fn) => fn(tx)),
95
+ }
96
+
97
+ await createSeedDocument(db, 'admin-1', {
98
+ collection: 'pages',
99
+ status: 'PUBLISHED',
100
+ data: {
101
+ title: 'Home',
102
+ slug: 'home',
103
+ content: '<p>Hello</p><script>alert(1)</script>',
104
+ },
105
+ })
106
+
107
+ expect(tx.document.create).toHaveBeenCalledWith({
108
+ data: expect.objectContaining({
109
+ collection: 'pages',
110
+ title: 'Home',
111
+ slug: 'home',
112
+ status: 'PUBLISHED',
113
+ publishedAt: expect.any(Date),
114
+ createdById: 'admin-1',
115
+ updatedById: 'admin-1',
116
+ plainText: expect.any(String),
117
+ contentHash: expect.any(String),
118
+ data: expect.objectContaining({
119
+ content: '<p>Hello</p>',
120
+ }),
121
+ }),
122
+ })
123
+ expect(tx.version.create).toHaveBeenCalledWith({
124
+ data: expect.objectContaining({
125
+ documentId: 'doc-1',
126
+ changedById: 'admin-1',
127
+ changeType: 'CREATE',
128
+ }),
129
+ })
130
+ })
131
+ })
132
+
133
+ describe('seed document upsert', () => {
134
+ function makeDb(existing: { id: string; status: string; publishedAt: Date | null } | null) {
135
+ const tx = {
136
+ document: {
137
+ create: vi.fn().mockResolvedValue({ id: 'doc-new' }),
138
+ update: vi.fn().mockResolvedValue({ id: existing?.id ?? 'doc-new' }),
139
+ },
140
+ version: { create: vi.fn() },
141
+ }
142
+ const db = {
143
+ document: { findFirst: vi.fn().mockResolvedValue(existing) },
144
+ $transaction: vi.fn(async (fn: any) => fn(tx)),
145
+ }
146
+ return { db, tx }
147
+ }
148
+
149
+ const doc = {
150
+ collection: 'pages',
151
+ status: 'PUBLISHED',
152
+ data: { title: 'Home v2', slug: 'home' },
153
+ }
154
+
155
+ it('updates an existing document matched by collection + slug', async () => {
156
+ const publishedAt = new Date('2026-01-01T00:00:00Z')
157
+ const { db, tx } = makeDb({ id: 'doc-1', status: 'PUBLISHED', publishedAt })
158
+
159
+ const result = await createSeedDocument(db, 'admin-1', doc, { upsert: true })
160
+
161
+ expect(result).toBe('updated')
162
+ expect(db.document.findFirst).toHaveBeenCalledWith({
163
+ where: { collection: 'pages', slug: 'home', deletedAt: null },
164
+ select: { id: true, status: true, publishedAt: true },
165
+ })
166
+ expect(tx.document.create).not.toHaveBeenCalled()
167
+ expect(tx.document.update).toHaveBeenCalledWith({
168
+ where: { id: 'doc-1' },
169
+ data: expect.objectContaining({
170
+ title: 'Home v2',
171
+ status: 'PUBLISHED',
172
+ // Re-publishing an already-published doc keeps the original timestamp.
173
+ publishedAt,
174
+ updatedById: 'admin-1',
175
+ }),
176
+ })
177
+ expect(tx.version.create).toHaveBeenCalledWith({
178
+ data: expect.objectContaining({ documentId: 'doc-1', changeType: 'UPDATE' }),
179
+ })
180
+ })
181
+
182
+ it('stamps publishedAt on the transition into PUBLISHED', async () => {
183
+ const { db, tx } = makeDb({ id: 'doc-1', status: 'DRAFT', publishedAt: null })
184
+
185
+ await createSeedDocument(db, 'admin-1', doc, { upsert: true })
186
+
187
+ expect(tx.document.update).toHaveBeenCalledWith(
188
+ expect.objectContaining({
189
+ data: expect.objectContaining({ publishedAt: expect.any(Date) }),
190
+ }),
191
+ )
192
+ })
193
+
194
+ it('creates when no document matches', async () => {
195
+ const { db, tx } = makeDb(null)
196
+
197
+ const result = await createSeedDocument(db, 'admin-1', doc, { upsert: true })
198
+
199
+ expect(result).toBe('created')
200
+ expect(tx.document.create).toHaveBeenCalled()
201
+ expect(tx.document.update).not.toHaveBeenCalled()
202
+ })
203
+
204
+ it('always creates slugless documents (no upsert key)', async () => {
205
+ const { db, tx } = makeDb({ id: 'doc-1', status: 'DRAFT', publishedAt: null })
206
+
207
+ const result = await createSeedDocument(
208
+ db,
209
+ 'admin-1',
210
+ { collection: 'pages', status: 'DRAFT', data: { title: 'No slug' } },
211
+ { upsert: true },
212
+ )
213
+
214
+ expect(result).toBe('created')
215
+ expect(db.document.findFirst).not.toHaveBeenCalled()
216
+ expect(tx.document.create).toHaveBeenCalled()
217
+ })
218
+
219
+ it('does not look up existing documents without the upsert flag', async () => {
220
+ const { db, tx } = makeDb({ id: 'doc-1', status: 'DRAFT', publishedAt: null })
221
+
222
+ const result = await createSeedDocument(db, 'admin-1', doc)
223
+
224
+ expect(result).toBe('created')
225
+ expect(db.document.findFirst).not.toHaveBeenCalled()
226
+ expect(tx.document.create).toHaveBeenCalled()
227
+ })
228
+ })
229
+
230
+ describe('demo navigations', () => {
231
+ // The scaffold site layout fetches menus by the `main` and `footer` slugs, so
232
+ // the demo seed must provide exactly those — guard against slug drift.
233
+ it('seeds header (main) and footer menus with well-formed items', () => {
234
+ const bySlug = new Map(DEMO_NAVIGATIONS.map((n) => [n.slug, n]))
235
+ expect(bySlug.has('main')).toBe(true)
236
+ expect(bySlug.has('footer')).toBe(true)
237
+ expect(bySlug.get('main')?.location).toBe('header')
238
+ expect(bySlug.get('footer')?.location).toBe('footer')
239
+
240
+ for (const nav of DEMO_NAVIGATIONS) {
241
+ expect(nav.items.length).toBeGreaterThan(0)
242
+ for (const item of nav.items) {
243
+ expect(typeof item.label).toBe('string')
244
+ expect(item.label.length).toBeGreaterThan(0)
245
+ expect(item.url.startsWith('/')).toBe(true)
246
+ }
247
+ }
248
+ })
249
+ })
@@ -0,0 +1,44 @@
1
+ import { afterEach, describe, expect, it, vi } from 'vitest'
2
+
3
+ import { fetchLatestVersions } from '../commands/upgrade.js'
4
+
5
+ // Regression for `prenta upgrade` hanging indefinitely on Windows: Node's fetch
6
+ // has no default timeout, so a connection that's accepted but never answered
7
+ // (stateful firewall / VPN) hangs the command with no output. Every network call
8
+ // must carry an abort timeout and fail fast into a graceful fallback.
9
+ describe('fetchLatestVersions', () => {
10
+ const realFetch = global.fetch
11
+ afterEach(() => {
12
+ global.fetch = realFetch
13
+ vi.restoreAllMocks()
14
+ })
15
+
16
+ it('passes an AbortSignal (timeout) to every fetch — no unbounded requests', async () => {
17
+ const opts: Array<{ signal?: unknown }> = []
18
+ global.fetch = vi.fn(async (_url: unknown, o: { signal?: unknown } = {}) => {
19
+ opts.push(o)
20
+ throw new Error('network down')
21
+ }) as unknown as typeof fetch
22
+ await fetchLatestVersions(['@prenta/core'])
23
+ expect(opts.length).toBeGreaterThanOrEqual(1)
24
+ for (const o of opts) expect(o.signal).toBeInstanceOf(AbortSignal)
25
+ })
26
+
27
+ it('returns {} gracefully when all fetches time out (never hangs or throws)', async () => {
28
+ global.fetch = vi.fn(async () => {
29
+ throw new DOMException('The operation timed out.', 'TimeoutError')
30
+ }) as unknown as typeof fetch
31
+ await expect(fetchLatestVersions(['@prenta/core'])).resolves.toEqual({})
32
+ })
33
+
34
+ it('uses update-server versions when it responds (and skips npm)', async () => {
35
+ global.fetch = vi.fn(async (url: unknown) => {
36
+ if (String(url).includes('/api/versions')) {
37
+ return { ok: true, json: async () => ({ '@prenta/core': '0.91.1' }) }
38
+ }
39
+ throw new Error('should not hit the npm registry')
40
+ }) as unknown as typeof fetch
41
+ const versions = await fetchLatestVersions(['@prenta/core'])
42
+ expect(versions).toEqual({ '@prenta/core': '0.91.1' })
43
+ })
44
+ })
@@ -0,0 +1,65 @@
1
+ import { describe, expect, it } from 'vitest'
2
+
3
+ import type { VercelEnvVar } from '../vercel/client.js'
4
+ import { buildEnvMatrix, evaluateBlobLink, presenceForKey } from '../vercel/env-matrix.js'
5
+
6
+ function env(key: string, target: VercelEnvVar['target']): VercelEnvVar {
7
+ return { key, target }
8
+ }
9
+
10
+ describe('presenceForKey', () => {
11
+ it('aggregates targets across multiple entries for the same key', () => {
12
+ const vars = [env('DATABASE_URL', ['production']), env('DATABASE_URL', ['preview'])]
13
+ expect(presenceForKey(vars, 'DATABASE_URL')).toEqual({
14
+ production: true,
15
+ preview: true,
16
+ development: false,
17
+ })
18
+ })
19
+ })
20
+
21
+ describe('buildEnvMatrix', () => {
22
+ it('flags required vars missing on production or preview as critical', () => {
23
+ const vars = [
24
+ env('DATABASE_URL', ['production', 'preview', 'development']),
25
+ env('CMS_SECRET', ['production']),
26
+ ]
27
+ const matrix = buildEnvMatrix(vars, ['DATABASE_URL', 'CMS_SECRET'], ['CRON_SECRET'])
28
+
29
+ expect(matrix.missingCritical).toEqual([{ key: 'CMS_SECRET', targets: ['preview'] }])
30
+ expect(matrix.rows.find((r) => r.key === 'CRON_SECRET')).toMatchObject({ required: false })
31
+ })
32
+ })
33
+
34
+ describe('evaluateBlobLink', () => {
35
+ it('reports none when no blob vars exist', () => {
36
+ expect(evaluateBlobLink([env('DATABASE_URL', ['production'])]).status).toBe('none')
37
+ })
38
+
39
+ it('reports ok when Marketplace store id is on production/preview (OIDC at runtime)', () => {
40
+ const report = evaluateBlobLink([
41
+ env('BLOB_STORE_ID', ['production', 'preview', 'development']),
42
+ ])
43
+ expect(report.linked).toBe(true)
44
+ expect(report.status).toBe('ok')
45
+ expect(report.missingTargets).toEqual([])
46
+ })
47
+
48
+ it('reports partial when linked but store id missing on production/preview', () => {
49
+ const report = evaluateBlobLink([
50
+ env('BLOB_WEBHOOK_PUBLIC_KEY', ['production', 'preview', 'development']),
51
+ ])
52
+ expect(report.linked).toBe(true)
53
+ expect(report.status).toBe('partial')
54
+ expect(report.missingTargets).toContain('production')
55
+ })
56
+
57
+ it('reports ok when token covers production and preview (dev optional)', () => {
58
+ const report = evaluateBlobLink([
59
+ env('BLOB_STORE_ID', ['production', 'preview']),
60
+ env('BLOB_READ_WRITE_TOKEN', ['production', 'preview']),
61
+ ])
62
+ expect(report.status).toBe('ok')
63
+ expect(report.missingTargets).toEqual(['development'])
64
+ })
65
+ })