@sanqianx/project-knowledge 4.7.0 → 5.0.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 (196) hide show
  1. package/.agents/plugins/marketplace.json +17 -17
  2. package/.claude-plugin/marketplace.json +1 -1
  3. package/CHANGELOG.md +836 -799
  4. package/README.md +58 -41
  5. package/_modules/knowledge-engine/README.md +91 -0
  6. package/_modules/knowledge-engine/package.json +38 -0
  7. package/_modules/knowledge-engine/src/analysis-contract.js +25 -0
  8. package/_modules/knowledge-engine/src/analysis-worker.js +54 -0
  9. package/_modules/knowledge-engine/src/bin.js +52 -0
  10. package/_modules/knowledge-engine/src/bridge-client.js +24 -0
  11. package/_modules/knowledge-engine/src/coordinator.js +263 -0
  12. package/_modules/knowledge-engine/src/headless-analyzer.js +75 -0
  13. package/_modules/knowledge-engine/src/memories.js +85 -0
  14. package/_modules/knowledge-engine/src/retrieval.js +116 -0
  15. package/_modules/knowledge-engine/src/runtime/README.md +7 -0
  16. package/_modules/knowledge-engine/src/runtime/atomic-file.js +204 -0
  17. package/_modules/knowledge-engine/src/runtime/automation-config.js +140 -0
  18. package/_modules/knowledge-engine/src/runtime/commit-conversation-binder.js +249 -0
  19. package/_modules/knowledge-engine/src/runtime/commit-processing-ledger.js +26 -0
  20. package/_modules/knowledge-engine/src/runtime/commit-prompt.js +202 -0
  21. package/_modules/knowledge-engine/src/runtime/commit-reconciler.js +432 -0
  22. package/_modules/knowledge-engine/src/runtime/content-hash.js +4 -0
  23. package/_modules/knowledge-engine/src/runtime/contracts.js +279 -0
  24. package/_modules/knowledge-engine/src/runtime/conversation-exclusions.js +204 -0
  25. package/_modules/knowledge-engine/src/runtime/conversation-store.js +387 -0
  26. package/_modules/knowledge-engine/src/runtime/evidence-bundle.js +228 -0
  27. package/_modules/knowledge-engine/src/runtime/git-runner.js +62 -0
  28. package/_modules/knowledge-engine/src/runtime/knowledge-promotion.js +444 -0
  29. package/_modules/knowledge-engine/src/runtime/knowledge-retrieval-service.js +402 -0
  30. package/_modules/knowledge-engine/src/runtime/layout.js +44 -0
  31. package/_modules/knowledge-engine/src/runtime/markdown-knowledge-indexer.js +230 -0
  32. package/_modules/knowledge-engine/src/runtime/scanner.js +298 -0
  33. package/_modules/knowledge-engine/src/server.js +154 -0
  34. package/_modules/knowledge-engine/src/store.js +236 -0
  35. package/_modules/knowledge-engine/ui/app.css +172 -0
  36. package/_modules/knowledge-engine/ui/app.js +297 -0
  37. package/_modules/knowledge-engine/ui/index.html +95 -0
  38. package/_site/README.md +30 -30
  39. package/_site/_test/ai-profile-resolver-test.js +137 -137
  40. package/_site/_test/automation-queue-test.js +14 -14
  41. package/_site/_test/automation-ui-test.js +40 -40
  42. package/_site/_test/background-task-registry-test.js +43 -43
  43. package/_site/_test/baseline-schema-test.js +91 -91
  44. package/_site/_test/chat-claudecodeui-match-test.js +92 -92
  45. package/_site/_test/claude-executable-discovery-test.js +62 -62
  46. package/_site/_test/claude-workbench-test.js +170 -170
  47. package/_site/_test/codex-conversation-projection-test.js +2 -1
  48. package/_site/_test/commit-conversation-binding-test.js +95 -95
  49. package/_site/_test/commit-evidence-test.js +110 -110
  50. package/_site/_test/conversation-api-test.js +109 -109
  51. package/_site/_test/conversation-store-test.js +136 -136
  52. package/_site/_test/desktop-browser-compat-test.js +28 -28
  53. package/_site/_test/desktop-hook-runtime-regression-test.js +204 -204
  54. package/_site/_test/explicit-commit-processor-test.js +37 -37
  55. package/_site/_test/fixtures/make-git-repos.js +12 -1
  56. package/_site/_test/folder-picker-output-test.js +19 -19
  57. package/_site/_test/fresh-hook-reimport-test.js +53 -0
  58. package/_site/_test/fresh-product-data-test.js +42 -0
  59. package/_site/_test/full-integration-e2e-test.js +165 -165
  60. package/_site/_test/git-validation-test.js +2 -2
  61. package/_site/_test/hook-runtime-endpoint-test.js +88 -88
  62. package/_site/_test/hook-status-repair-api-test.js +152 -152
  63. package/_site/_test/hook-trigger-test.js +85 -85
  64. package/_site/_test/import-preflight-api-test.js +234 -233
  65. package/_site/_test/index-writer-concurrency-test.js +135 -135
  66. package/_site/_test/integration-adapters-test.js +171 -171
  67. package/_site/_test/integration-surface-coverage-test.js +164 -164
  68. package/_site/_test/knowledge-language-control-test.js +170 -170
  69. package/_site/_test/knowledge-promotion-recovery-test.js +260 -260
  70. package/_site/_test/knowledge-query-test.js +54 -54
  71. package/_site/_test/knowledge-retrieval-service-test.js +77 -77
  72. package/_site/_test/knowledge-storage-startup-test.js +52 -52
  73. package/_site/_test/knowledge-store-logs-supervision-test.js +89 -89
  74. package/_site/_test/logging-api-test.js +89 -89
  75. package/_site/_test/logging-sse-no-gap-test.js +88 -88
  76. package/_site/_test/logging-ui-test.js +106 -104
  77. package/_site/_test/markdown-delta-overlay-test.js +85 -85
  78. package/_site/_test/markdown-maintenance-api-test.js +75 -75
  79. package/_site/_test/mcp-server-test.js +149 -149
  80. package/_site/_test/module-artifact-integrity-test.js +27 -0
  81. package/_site/_test/module-boundary-test.js +30 -0
  82. package/_site/_test/module-bridge-eventbridge-test.js +7 -0
  83. package/_site/_test/module-model-configuration-test.js +44 -0
  84. package/_site/_test/module-process-lifecycle-test.js +24 -0
  85. package/_site/_test/module-stream-proxy-test.js +34 -0
  86. package/_site/_test/non-release-ci-test.js +34 -34
  87. package/_site/_test/offline-boundary-isolation-test.js +32 -32
  88. package/_site/_test/packaged-ui-smoke-test.js +11 -9
  89. package/_site/_test/path-consistency-test.js +151 -151
  90. package/_site/_test/pending-sweep-test.js +9 -9
  91. package/_site/_test/post-commit-automation-test.js +87 -87
  92. package/_site/_test/product-diagnostics-ui-test.js +75 -0
  93. package/_site/_test/product-import-ui-test.js +92 -0
  94. package/_site/_test/project-delete-recovery-test.js +64 -64
  95. package/_site/_test/project-goal-editor-test.js +144 -144
  96. package/_site/_test/project-lifecycle-transaction-test.js +106 -106
  97. package/_site/_test/prompt-settings-test.js +115 -115
  98. package/_site/_test/protected-architecture-gate-test.js +122 -122
  99. package/_site/_test/refactor-characterization-test.js +36 -36
  100. package/_site/_test/release-version-sync-test.js +1 -1
  101. package/_site/_test/requirement-recorder-test.js +173 -173
  102. package/_site/_test/run-all-tests.js +159 -156
  103. package/_site/_test/shared-contracts-test.js +58 -58
  104. package/_site/_test/startup-analysis-disabled-test.js +10 -10
  105. package/_site/_test/storage-foundation-test.js +78 -78
  106. package/_site/_test/structured-logger-test.js +101 -101
  107. package/_site/_test/tracking-start-test.js +109 -108
  108. package/_site/_test/workbench-permission-test.js +117 -117
  109. package/_site/_test/workspace-ui-contract-test.js +28 -43
  110. package/_site/lib/ai-profile-resolver.js +78 -78
  111. package/_site/lib/ai-workspace.js +101 -101
  112. package/_site/lib/automation-config.js +140 -140
  113. package/_site/lib/bridge-adapter.js +1 -0
  114. package/_site/lib/bridge-consumer-service.js +402 -402
  115. package/_site/lib/claude-cli-runner.js +1510 -1510
  116. package/_site/lib/commit-conversation-binder.js +234 -234
  117. package/_site/lib/commit-processing-ledger.js +27 -27
  118. package/_site/lib/commit-prompt.js +202 -202
  119. package/_site/lib/commit-reconciler.js +406 -406
  120. package/_site/lib/contracts.js +284 -279
  121. package/_site/lib/conversation-query-service.js +144 -144
  122. package/_site/lib/conversation-store.js +385 -385
  123. package/_site/lib/data-dir.js +6 -74
  124. package/_site/lib/engine-connection.js +67 -0
  125. package/_site/lib/evidence-bundle.js +228 -228
  126. package/_site/lib/folder-picker-output.js +21 -21
  127. package/_site/lib/git-runner.js +62 -62
  128. package/_site/lib/github-team-store.js +1172 -1172
  129. package/_site/lib/hook-manager.js +203 -202
  130. package/_site/lib/index-service.js +173 -173
  131. package/_site/lib/integration-installer.js +908 -907
  132. package/_site/lib/kb-framework.js +176 -176
  133. package/_site/lib/kb-validator.js +124 -124
  134. package/_site/lib/knowledge-promotion.js +435 -435
  135. package/_site/lib/knowledge-retrieval-service.js +401 -401
  136. package/_site/lib/knowledge-tool-runtime.js +359 -359
  137. package/_site/lib/llm-client.js +161 -161
  138. package/_site/lib/markdown-knowledge-indexer.js +230 -230
  139. package/_site/lib/module-bridge.js +191 -36
  140. package/_site/lib/post-commit-automation.js +87 -87
  141. package/_site/lib/product-data.js +24 -0
  142. package/_site/lib/project-lifecycle-service.js +535 -497
  143. package/_site/lib/project-store.js +268 -262
  144. package/_site/lib/requirement-recorder.js +278 -278
  145. package/_site/lib/runtime-endpoint.js +155 -155
  146. package/_site/lib/scanner.js +298 -298
  147. package/_site/lib/server-app.js +1579 -1520
  148. package/_site/lib/settings-store.js +133 -113
  149. package/_site/lib/storage-layout.js +162 -162
  150. package/_site/lib/structured-logger.js +574 -574
  151. package/_site/scripts/folder-picker.ps1 +156 -156
  152. package/_site/scripts/hook-trigger.js +157 -156
  153. package/_site/scripts/install-module-candidates.js +69 -0
  154. package/_site/scripts/pack-module-candidates.js +72 -0
  155. package/_site/scripts/sync-release-version.js +2 -2
  156. package/_site/scripts/vendor-modules.js +47 -0
  157. package/_site/scripts/verify-module-runtime.js +45 -0
  158. package/_site/scripts/verify-product-runtime.js +34 -0
  159. package/bin/project-knowledge-mcp.js +194 -194
  160. package/docs/README.zh-CN.md +42 -29
  161. package/docs/fresh-v5-start.md +28 -0
  162. package/docs/project-registry-schema.md +22 -22
  163. package/docs/testing-strategy.md +33 -33
  164. package/module-runtime-manifest.json +220 -0
  165. package/package.json +14 -9
  166. package/plugins/project-knowledge/.claude-plugin/plugin.json +1 -1
  167. package/plugins/project-knowledge/.codex-plugin/plugin.json +1 -1
  168. package/plugins/project-knowledge/.mcp.json +1 -1
  169. package/plugins/project-knowledge/opencode/project-knowledge.md +3 -3
  170. package/plugins/project-knowledge/skills/project-knowledge/SKILL.md +28 -28
  171. package/templates/project-readme.md +34 -34
  172. package/ui/favicon.svg +38 -38
  173. package/ui/index.html +52 -149
  174. package/ui/product.css +9 -0
  175. package/ui/product.js +412 -0
  176. package/vendor-manifest.json +45 -0
  177. package/_site/_test/data-dir-migration-test.js +0 -77
  178. package/_site/_test/import-ui-flow-test.js +0 -171
  179. package/_site/_test/knowledge-migration-test.js +0 -75
  180. package/_site/_test/legacy-forward-compat-test.js +0 -257
  181. package/_site/_test/legacy-project-upgrade-e2e-test.js +0 -363
  182. package/_site/_test/p0-data-migration-characterization-test.js +0 -37
  183. package/_site/_test/p0-e2e-gate-test.js +0 -347
  184. package/_site/_test/project-control-panel-task14-test.js +0 -63
  185. package/_site/_test/project-layout-v2-migration-test.js +0 -127
  186. package/_site/_test/server-runtime-migration-safety-test.js +0 -27
  187. package/_site/_test/task15-20-ui-flow-test.js +0 -148
  188. package/_site/_test/ui-i18n-toggle-test.js +0 -114
  189. package/_site/_test/ui-smoke-test.js +0 -73
  190. package/_site/_test/v4122-upgrade-data-contract-test.js +0 -56
  191. package/_site/lib/data-state-classifier.js +0 -40
  192. package/_site/lib/legacy-data-manifest.js +0 -26
  193. package/_site/lib/migration-service.js +0 -442
  194. package/ui/app.css +0 -58
  195. package/ui/app.js +0 -607
  196. package/ui/i18n.js +0 -146
@@ -1,233 +1,234 @@
1
- // _site/_test/import-preflight-api-test.js
2
- //
3
- // T06: preflight endpoint surface contract.
4
- // Drives POST /api/projects/preflight-import through every documented
5
- // prerequisite path and asserts the response shape.
6
-
7
- const assert = require('assert');
8
- const fs = require('fs');
9
- const os = require('os');
10
- const path = require('path');
11
- const { spawnSync } = require('child_process');
12
- const { spawnServer } = require('./helpers/spawn-server');
13
-
14
- const ROOT = path.resolve(__dirname, '..', '..');
15
- const PORT = 7903;
16
- const BASE = `http://127.0.0.1:${PORT}`;
17
- const dataDir = fs.mkdtempSync(path.join(os.tmpdir(), `pk-preflight-${process.pid}-`));
18
-
19
- function git(repo, args) {
20
- const result = spawnSync('git', ['-C', repo, ...args], {
21
- encoding: 'utf8',
22
- windowsHide: true,
23
- env: { ...process.env, GIT_TERMINAL_PROMPT: '0' },
24
- });
25
- if (result.status !== 0) throw new Error(result.stderr || `git ${args.join(' ')} failed: ${result.status}`);
26
- return String(result.stdout || '').trim();
27
- }
28
-
29
- async function requestJson(method, pathname, body) {
30
- const response = await fetch(`${BASE}${pathname}`, {
31
- method,
32
- headers: body === undefined ? undefined : { 'Content-Type': 'application/json' },
33
- body: body === undefined ? undefined : JSON.stringify(body),
34
- });
35
- return { status: response.status, body: await response.json() };
36
- }
37
- function postJson(pathname, body) { return requestJson('POST', pathname, body); }
38
- function patchJson(pathname, body) { return requestJson('PATCH', pathname, body); }
39
- function putJson(pathname, body) { return requestJson('PUT', pathname, body); }
40
-
41
- async function waitForServer() {
42
- for (let attempt = 0; attempt < 100; attempt += 1) {
43
- try { if ((await fetch(`${BASE}/api/health`)).ok) return; } catch {}
44
- await new Promise(resolve => setTimeout(resolve, 100));
45
- }
46
- throw new Error('server did not start');
47
- }
48
-
49
- (async () => {
50
- const repoOk = fs.mkdtempSync(path.join(os.tmpdir(), `pk-preflight-repo-ok-${process.pid}-`));
51
- const repoEmpty = fs.mkdtempSync(path.join(os.tmpdir(), `pk-preflight-repo-empty-${process.pid}-`));
52
- const repoNonGit = fs.mkdtempSync(path.join(os.tmpdir(), `pk-preflight-repo-non-git-${process.pid}-`));
53
- const repoSpaces = path.join(fs.mkdtempSync(path.join(os.tmpdir(), `pk-preflight-spaces-${process.pid}-`)), 'project with spaces');
54
- const repoThirdParty = fs.mkdtempSync(path.join(os.tmpdir(), `pk-preflight-thirdparty-${process.pid}-`));
55
- try {
56
- fs.mkdirSync(repoSpaces, { recursive: true });
57
- // existing Git repo with a baseline commit
58
- git(repoOk, ['init', '-q', '-b', 'main']);
59
- git(repoOk, ['config', 'user.email', 'preflight@example.local']);
60
- git(repoOk, ['config', 'user.name', 'Preflight']);
61
- fs.writeFileSync(path.join(repoOk, 'README.md'), '# ok\n');
62
- git(repoOk, ['add', 'README.md']);
63
- git(repoOk, ['commit', '-q', '-m', 'baseline']);
64
- // empty git repo (no commits yet)
65
- git(repoEmpty, ['init', '-q', '-b', 'main']);
66
- git(repoEmpty, ['config', 'user.email', 'preflight@example.local']);
67
- git(repoEmpty, ['config', 'user.name', 'Preflight']);
68
- // non-git dir with a stray file
69
- fs.writeFileSync(path.join(repoNonGit, 'note.txt'), 'hi\n');
70
- // repo with a literal space in its path
71
- git(repoSpaces, ['init', '-q', '-b', 'main']);
72
- git(repoSpaces, ['config', 'user.email', 'preflight@example.local']);
73
- git(repoSpaces, ['config', 'user.name', 'Preflight']);
74
- fs.writeFileSync(path.join(repoSpaces, 'README.md'), '# spaces\n');
75
- git(repoSpaces, ['add', 'README.md']);
76
- git(repoSpaces, ['commit', '-q', '-m', 'baseline']);
77
- // repo with a third-party hook
78
- git(repoThirdParty, ['init', '-q', '-b', 'main']);
79
- git(repoThirdParty, ['config', 'user.email', 'preflight@example.local']);
80
- git(repoThirdParty, ['config', 'user.name', 'Preflight']);
81
- fs.writeFileSync(path.join(repoThirdParty, 'README.md'), '# tp\n');
82
- git(repoThirdParty, ['add', 'README.md']);
83
- git(repoThirdParty, ['commit', '-q', '-m', 'baseline']);
84
- const gitDir = git(repoThirdParty, ['rev-parse', '--path-format=absolute', '--git-dir']);
85
- const hooksDir = path.join(path.resolve(gitDir), 'hooks');
86
- fs.mkdirSync(hooksDir, { recursive: true });
87
- fs.writeFileSync(path.join(hooksDir, 'post-commit'), '#!/bin/sh\necho "user owned"\nexit 0\n', { mode: 0o755 });
88
-
89
- const spawned = spawnServer({
90
- root: ROOT, port: PORT, dataDir, tag: 'preflight',
91
- extraEnv: { KB_SKIP_MIGRATION: '1' },
92
- });
93
- try {
94
- await waitForServer();
95
- const knowledgeRoot = path.join(dataDir, 'knowledge');
96
- fs.mkdirSync(knowledgeRoot, { recursive: true });
97
- let r = await patchJson('/api/settings', { knowledge: { rootPath: knowledgeRoot } });
98
- assert.strictEqual(r.status, 200, JSON.stringify(r.body));
99
- // Configure a fake AI profile so most checks succeed.
100
- r = await putJson('/api/ai-profiles', {
101
- schema: 'ai-profiles/v1', defaultProfileId: 'fake',
102
- profiles: [{ id: 'fake', name: 'Fake', enabled: true, vendor: 'anthropic', model: 'fake', apiKeyUpdate: { mode: 'replace', value: 'k' } }],
103
- });
104
- assert.strictEqual(r.status, 200, JSON.stringify(r.body));
105
-
106
- // Case 1: existing Git repo with AI profile -> ready
107
- r = await postJson('/api/projects/preflight-import', { localPath: repoOk, aiProfileId: 'fake' });
108
- assert.strictEqual(r.status, 200);
109
- assert.strictEqual(r.body.ok, true);
110
- assert.strictEqual(r.body.ready, true);
111
- assert.strictEqual(r.body.plannedGitInit, false);
112
- assert.deepStrictEqual(r.body.problems, []);
113
- assert.strictEqual(r.body.checks.git.status, 'ok');
114
- assert.strictEqual(r.body.checks.aiProfile.ok, true);
115
- assert.strictEqual(r.body.effective.aiProfile.id, 'fake');
116
-
117
- // Case 2: non-Git folder -> plannedGitInit=true, ready=true (auto-init path)
118
- // The product intentionally auto-inits on import (T06), so the preflight
119
- // surfaces the planned init rather than blocking. The UI shows a banner.
120
- r = await postJson('/api/projects/preflight-import', { localPath: repoNonGit, aiProfileId: 'fake' });
121
- assert.strictEqual(r.status, 200);
122
- assert.strictEqual(r.body.plannedGitInit, true);
123
- assert.strictEqual(r.body.ready, true, 'non-git folder with planned auto-init is importable');
124
- assert.strictEqual(r.body.checks.git.plannedInit, true);
125
- assert.strictEqual(r.body.checks.git.status, 'non-git');
126
-
127
- // Case 3: empty git repo (HEAD missing) -> ready=true (commit will be made later)
128
- r = await postJson('/api/projects/preflight-import', { localPath: repoEmpty, aiProfileId: 'fake' });
129
- assert.strictEqual(r.status, 200);
130
- assert.strictEqual(r.body.ok, true, 'empty repo is importable; the import transaction creates the first commit semantics later');
131
- assert.strictEqual(r.body.checks.git.status, 'empty');
132
- assert.strictEqual(r.body.checks.git.headCommit, null);
133
-
134
- // Case 4: missing knowledge root -> ready=false
135
- const noKnowledgeSpawned = spawnServer({
136
- root: ROOT, port: PORT + 1, dataDir: fs.mkdtempSync(path.join(os.tmpdir(), `pk-preflight-nokb-${process.pid}-`)),
137
- tag: 'preflight-nokb', extraEnv: { KB_SKIP_MIGRATION: '1' },
138
- });
139
- try {
140
- for (let attempt = 0; attempt < 100; attempt += 1) {
141
- try { if ((await fetch(`http://127.0.0.1:${PORT + 1}/api/health`)).ok) break; } catch {}
142
- await new Promise(resolve => setTimeout(resolve, 100));
143
- }
144
- const r2 = await fetch(`http://127.0.0.1:${PORT + 1}/api/projects/preflight-import`, {
145
- method: 'POST', headers: { 'Content-Type': 'application/json' },
146
- body: JSON.stringify({ localPath: repoOk, aiProfileId: 'fake' }),
147
- }).then(async response => ({ status: response.status, body: await response.json() }));
148
- console.log('DEBUG no-knowledge-root:', r2.status, JSON.stringify(r2.body));
149
- assert.strictEqual(r2.status, 200);
150
- assert.strictEqual(r2.body.ready, false);
151
- assert(r2.body.problems.some(p => p.code === 'KNOWLEDGE_ROOT_MISSING'));
152
- } finally {
153
- noKnowledgeSpawned.child.kill();
154
- await new Promise(resolve => noKnowledgeSpawned.child.once('exit', resolve));
155
- }
156
-
157
- // Case 5: path with spaces -> ready=true
158
- r = await postJson('/api/projects/preflight-import', { localPath: repoSpaces, aiProfileId: 'fake' });
159
- assert.strictEqual(r.status, 200);
160
- assert.strictEqual(r.body.ready, true);
161
- assert.strictEqual(r.body.checks.git.ok, true);
162
-
163
- // Case 6: no AI profile configured -> ready=false with AI_PROFILE_REQUIRED
164
- const noProfileSpawned = spawnServer({
165
- root: ROOT, port: PORT + 2, dataDir: fs.mkdtempSync(path.join(os.tmpdir(), `pk-preflight-noprofile-${process.pid}-`)),
166
- tag: 'preflight-noprofile', extraEnv: { KB_SKIP_MIGRATION: '1' },
167
- });
168
- try {
169
- for (let attempt = 0; attempt < 100; attempt += 1) {
170
- try { if ((await fetch(`http://127.0.0.1:${PORT + 2}/api/health`)).ok) break; } catch {}
171
- await new Promise(resolve => setTimeout(resolve, 100));
172
- }
173
- // Patch only knowledge root; no AI profile configured
174
- const noProfileDir = noProfileSpawned.dataDir || noProfileSpawned.dir;
175
- await fetch(`http://127.0.0.1:${PORT + 2}/api/settings`, {
176
- method: 'PATCH', headers: { 'Content-Type': 'application/json' },
177
- body: JSON.stringify({ knowledge: { rootPath: path.join(noProfileDir, 'knowledge') } }),
178
- });
179
- const r2 = await fetch(`http://127.0.0.1:${PORT + 2}/api/projects/preflight-import`, {
180
- method: 'POST', headers: { 'Content-Type': 'application/json' },
181
- body: JSON.stringify({ localPath: repoOk }),
182
- }).then(async response => ({ status: response.status, body: await response.json() }));
183
- assert.strictEqual(r2.status, 200);
184
- assert.strictEqual(r2.body.ready, false);
185
- assert(r2.body.problems.some(p => p.code === 'AI_PROFILE_REQUIRED'));
186
- } finally {
187
- noProfileSpawned.child.kill();
188
- await new Promise(resolve => noProfileSpawned.child.once('exit', resolve));
189
- }
190
-
191
- // Case 7: third-party hook -> ready=false with HOOK_CONFLICT
192
- r = await postJson('/api/projects/preflight-import', { localPath: repoThirdParty, aiProfileId: 'fake' });
193
- assert.strictEqual(r.status, 200);
194
- assert.strictEqual(r.body.ready, false);
195
- assert(r.body.problems.some(p => p.code === 'HOOK_CONFLICT'));
196
- assert.strictEqual(r.body.checks.hook.ok, false);
197
- assert.strictEqual(r.body.checks.hook.reason, 'third-party');
198
-
199
- // Case 8: duplicate detection after a successful import
200
- r = await postJson('/api/projects/import', { localPath: repoOk, displayName: 'Dup', aiProfileId: 'fake' });
201
- assert.strictEqual(r.status, 201, JSON.stringify(r.body));
202
- const projectId = r.body.projectId;
203
- r = await postJson('/api/projects/preflight-import', { localPath: repoOk, aiProfileId: 'fake' });
204
- assert.strictEqual(r.status, 200);
205
- assert.strictEqual(r.body.ready, false);
206
- assert(r.body.problems.some(p => p.code === 'DUPLICATE_PROJECT' && p.existingProjectId === projectId));
207
-
208
- // Case 9: invalid knowledgeLanguage -> problem reported
209
- r = await postJson('/api/projects/preflight-import', { localPath: repoOk, aiProfileId: 'fake', knowledgeLanguage: 'xx-INVALID' });
210
- assert.strictEqual(r.status, 200);
211
- assert.strictEqual(r.body.ready, false);
212
- assert(r.body.problems.some(p => p.code === 'KNOWLEDGE_LANGUAGE_INVALID'));
213
-
214
- // Case 10: path missing entirely
215
- r = await postJson('/api/projects/preflight-import', { localPath: path.join(dataDir, 'does-not-exist') });
216
- assert.strictEqual(r.status, 200);
217
- assert.strictEqual(r.body.ready, false);
218
- assert(r.body.problems.some(p => p.code === 'PATH_MISSING'));
219
- } finally {
220
- spawned.child.kill();
221
- await new Promise(resolve => spawned.child.once('exit', resolve));
222
- }
223
- } finally {
224
- fs.rmSync(dataDir, { recursive: true, force: true });
225
- for (const dir of [repoOk, repoEmpty, repoNonGit, repoSpaces, repoThirdParty]) {
226
- try { fs.rmSync(dir, { recursive: true, force: true }); } catch {}
227
- }
228
- }
229
- console.log('import-preflight-api-test PASS');
230
- })().catch(error => {
231
- console.error(error.stack || error.message);
232
- process.exit(1);
233
- });
1
+ // _site/_test/import-preflight-api-test.js
2
+ //
3
+ // T06: preflight endpoint surface contract.
4
+ // Drives POST /api/projects/preflight-import through every documented
5
+ // prerequisite path and asserts the response shape.
6
+
7
+ const assert = require('assert');
8
+ const fs = require('fs');
9
+ const os = require('os');
10
+ const path = require('path');
11
+ const { spawnSync } = require('child_process');
12
+ const { spawnServer } = require('./helpers/spawn-server');
13
+
14
+ const ROOT = path.resolve(__dirname, '..', '..');
15
+ const PORT = 7903;
16
+ const BASE = `http://127.0.0.1:${PORT}`;
17
+ const dataDir = fs.mkdtempSync(path.join(os.tmpdir(), `pk-preflight-${process.pid}-`));
18
+
19
+ function git(repo, args) {
20
+ const result = spawnSync('git', ['-C', repo, ...args], {
21
+ encoding: 'utf8',
22
+ windowsHide: true,
23
+ env: { ...process.env, GIT_TERMINAL_PROMPT: '0' },
24
+ });
25
+ if (result.status !== 0) throw new Error(result.stderr || `git ${args.join(' ')} failed: ${result.status}`);
26
+ return String(result.stdout || '').trim();
27
+ }
28
+
29
+ async function requestJson(method, pathname, body) {
30
+ const response = await fetch(`${BASE}${pathname}`, {
31
+ method,
32
+ headers: body === undefined ? undefined : { 'Content-Type': 'application/json' },
33
+ body: body === undefined ? undefined : JSON.stringify(body),
34
+ });
35
+ return { status: response.status, body: await response.json() };
36
+ }
37
+ function postJson(pathname, body) { return requestJson('POST', pathname, body); }
38
+ function patchJson(pathname, body) { return requestJson('PATCH', pathname, body); }
39
+ function putJson(pathname, body) { return requestJson('PUT', pathname, body); }
40
+
41
+ async function waitForServer() {
42
+ for (let attempt = 0; attempt < 100; attempt += 1) {
43
+ try { if ((await fetch(`${BASE}/api/health`)).ok) return; } catch {}
44
+ await new Promise(resolve => setTimeout(resolve, 100));
45
+ }
46
+ throw new Error('server did not start');
47
+ }
48
+
49
+ (async () => {
50
+ const repoOk = fs.mkdtempSync(path.join(os.tmpdir(), `pk-preflight-repo-ok-${process.pid}-`));
51
+ const repoEmpty = fs.mkdtempSync(path.join(os.tmpdir(), `pk-preflight-repo-empty-${process.pid}-`));
52
+ const repoNonGit = fs.mkdtempSync(path.join(os.tmpdir(), `pk-preflight-repo-non-git-${process.pid}-`));
53
+ const repoSpaces = path.join(fs.mkdtempSync(path.join(os.tmpdir(), `pk-preflight-spaces-${process.pid}-`)), 'project with spaces');
54
+ const repoThirdParty = fs.mkdtempSync(path.join(os.tmpdir(), `pk-preflight-thirdparty-${process.pid}-`));
55
+ try {
56
+ fs.mkdirSync(repoSpaces, { recursive: true });
57
+ // existing Git repo with a baseline commit
58
+ git(repoOk, ['init', '-q', '-b', 'main']);
59
+ git(repoOk, ['config', 'user.email', 'preflight@example.local']);
60
+ git(repoOk, ['config', 'user.name', 'Preflight']);
61
+ fs.writeFileSync(path.join(repoOk, 'README.md'), '# ok\n');
62
+ git(repoOk, ['add', 'README.md']);
63
+ git(repoOk, ['commit', '-q', '-m', 'baseline']);
64
+ // empty git repo (no commits yet)
65
+ git(repoEmpty, ['init', '-q', '-b', 'main']);
66
+ git(repoEmpty, ['config', 'user.email', 'preflight@example.local']);
67
+ git(repoEmpty, ['config', 'user.name', 'Preflight']);
68
+ // non-git dir with a stray file
69
+ fs.writeFileSync(path.join(repoNonGit, 'note.txt'), 'hi\n');
70
+ // repo with a literal space in its path
71
+ git(repoSpaces, ['init', '-q', '-b', 'main']);
72
+ git(repoSpaces, ['config', 'user.email', 'preflight@example.local']);
73
+ git(repoSpaces, ['config', 'user.name', 'Preflight']);
74
+ fs.writeFileSync(path.join(repoSpaces, 'README.md'), '# spaces\n');
75
+ git(repoSpaces, ['add', 'README.md']);
76
+ git(repoSpaces, ['commit', '-q', '-m', 'baseline']);
77
+ // repo with a third-party hook
78
+ git(repoThirdParty, ['init', '-q', '-b', 'main']);
79
+ git(repoThirdParty, ['config', 'user.email', 'preflight@example.local']);
80
+ git(repoThirdParty, ['config', 'user.name', 'Preflight']);
81
+ fs.writeFileSync(path.join(repoThirdParty, 'README.md'), '# tp\n');
82
+ git(repoThirdParty, ['add', 'README.md']);
83
+ git(repoThirdParty, ['commit', '-q', '-m', 'baseline']);
84
+ const gitDir = git(repoThirdParty, ['rev-parse', '--path-format=absolute', '--git-dir']);
85
+ const hooksDir = path.join(path.resolve(gitDir), 'hooks');
86
+ fs.mkdirSync(hooksDir, { recursive: true });
87
+ fs.writeFileSync(path.join(hooksDir, 'post-commit'), '#!/bin/sh\necho "user owned"\nexit 0\n', { mode: 0o755 });
88
+
89
+ const spawned = spawnServer({
90
+ root: ROOT, port: PORT, dataDir, tag: 'preflight',
91
+ extraEnv: { KB_SKIP_MIGRATION: '1' },
92
+ });
93
+ try {
94
+ await waitForServer();
95
+ const knowledgeRoot = path.join(dataDir, 'knowledge');
96
+ fs.mkdirSync(knowledgeRoot, { recursive: true });
97
+ let r = await patchJson('/api/settings', { knowledge: { rootPath: knowledgeRoot } });
98
+ assert.strictEqual(r.status, 200, JSON.stringify(r.body));
99
+ // Configure a fake AI profile so most checks succeed.
100
+ r = await putJson('/api/ai-profiles', {
101
+ schema: 'ai-profiles/v1', defaultProfileId: 'fake',
102
+ profiles: [{ id: 'fake', name: 'Fake', enabled: true, vendor: 'anthropic', model: 'fake', apiKeyUpdate: { mode: 'replace', value: 'k' } }],
103
+ });
104
+ assert.strictEqual(r.status, 200, JSON.stringify(r.body));
105
+
106
+ // Case 1: existing Git repo with AI profile -> ready
107
+ r = await postJson('/api/projects/preflight-import', { localPath: repoOk, aiProfileId: 'fake' });
108
+ assert.strictEqual(r.status, 200);
109
+ assert.strictEqual(r.body.ok, true);
110
+ assert.strictEqual(r.body.ready, true);
111
+ assert.strictEqual(r.body.plannedGitInit, false);
112
+ assert.deepStrictEqual(r.body.problems, []);
113
+ assert.strictEqual(r.body.checks.git.status, 'ok');
114
+ assert.strictEqual(r.body.checks.aiProfile.ok, true);
115
+ assert.strictEqual(r.body.effective.aiProfile.id, 'fake');
116
+
117
+ // Case 2: non-Git folder -> plannedGitInit=true, ready=true (auto-init path)
118
+ // The product intentionally auto-inits on import (T06), so the preflight
119
+ // surfaces the planned init rather than blocking. The UI shows a banner.
120
+ r = await postJson('/api/projects/preflight-import', { localPath: repoNonGit, aiProfileId: 'fake' });
121
+ assert.strictEqual(r.status, 200);
122
+ assert.strictEqual(r.body.plannedGitInit, true);
123
+ assert.strictEqual(r.body.ready, true, 'non-git folder with planned auto-init is importable');
124
+ assert.strictEqual(r.body.checks.git.plannedInit, true);
125
+ assert.strictEqual(r.body.checks.git.status, 'non-git');
126
+
127
+ // Case 3: empty git repo (HEAD missing) -> ready=true (commit will be made later)
128
+ r = await postJson('/api/projects/preflight-import', { localPath: repoEmpty, aiProfileId: 'fake' });
129
+ assert.strictEqual(r.status, 200);
130
+ assert.strictEqual(r.body.ok, true, 'empty repo is importable; the import transaction creates the first commit semantics later');
131
+ assert.strictEqual(r.body.checks.git.status, 'empty');
132
+ assert.strictEqual(r.body.checks.git.headCommit, null);
133
+
134
+ // Case 4: missing knowledge root -> ready=false
135
+ const noKnowledgeSpawned = spawnServer({
136
+ root: ROOT, port: PORT + 1, dataDir: fs.mkdtempSync(path.join(os.tmpdir(), `pk-preflight-nokb-${process.pid}-`)),
137
+ tag: 'preflight-nokb', extraEnv: { KB_SKIP_MIGRATION: '1' },
138
+ });
139
+ try {
140
+ for (let attempt = 0; attempt < 100; attempt += 1) {
141
+ try { if ((await fetch(`http://127.0.0.1:${PORT + 1}/api/health`)).ok) break; } catch {}
142
+ await new Promise(resolve => setTimeout(resolve, 100));
143
+ }
144
+ const r2 = await fetch(`http://127.0.0.1:${PORT + 1}/api/projects/preflight-import`, {
145
+ method: 'POST', headers: { 'Content-Type': 'application/json' },
146
+ body: JSON.stringify({ localPath: repoOk, aiProfileId: 'fake' }),
147
+ }).then(async response => ({ status: response.status, body: await response.json() }));
148
+ console.log('DEBUG no-knowledge-root:', r2.status, JSON.stringify(r2.body));
149
+ assert.strictEqual(r2.status, 200);
150
+ assert.strictEqual(r2.body.ready, false);
151
+ assert(r2.body.problems.some(p => p.code === 'KNOWLEDGE_ROOT_MISSING'));
152
+ } finally {
153
+ noKnowledgeSpawned.child.kill();
154
+ await new Promise(resolve => noKnowledgeSpawned.child.once('exit', resolve));
155
+ }
156
+
157
+ // Case 5: path with spaces -> ready=true
158
+ r = await postJson('/api/projects/preflight-import', { localPath: repoSpaces, aiProfileId: 'fake' });
159
+ assert.strictEqual(r.status, 200);
160
+ assert.strictEqual(r.body.ready, true);
161
+ assert.strictEqual(r.body.checks.git.ok, true);
162
+
163
+ // Import establishes a baseline only; a missing model must not prevent registration.
164
+ const noProfileSpawned = spawnServer({
165
+ root: ROOT, port: PORT + 2, dataDir: fs.mkdtempSync(path.join(os.tmpdir(), `pk-preflight-noprofile-${process.pid}-`)),
166
+ tag: 'preflight-noprofile', extraEnv: { KB_SKIP_MIGRATION: '1' },
167
+ });
168
+ try {
169
+ for (let attempt = 0; attempt < 100; attempt += 1) {
170
+ try { if ((await fetch(`http://127.0.0.1:${PORT + 2}/api/health`)).ok) break; } catch {}
171
+ await new Promise(resolve => setTimeout(resolve, 100));
172
+ }
173
+ // Patch only knowledge root; no AI profile configured
174
+ const noProfileDir = noProfileSpawned.dataDir || noProfileSpawned.dir;
175
+ await fetch(`http://127.0.0.1:${PORT + 2}/api/settings`, {
176
+ method: 'PATCH', headers: { 'Content-Type': 'application/json' },
177
+ body: JSON.stringify({ knowledge: { rootPath: path.join(noProfileDir, 'knowledge') } }),
178
+ });
179
+ const r2 = await fetch(`http://127.0.0.1:${PORT + 2}/api/projects/preflight-import`, {
180
+ method: 'POST', headers: { 'Content-Type': 'application/json' },
181
+ body: JSON.stringify({ localPath: repoOk }),
182
+ }).then(async response => ({ status: response.status, body: await response.json() }));
183
+ assert.strictEqual(r2.status, 200);
184
+ assert.strictEqual(r2.body.ready, true);
185
+ assert.strictEqual(r2.body.checks.aiProfile.ok, false);
186
+ assert.strictEqual(r2.body.checks.aiProfile.required, false);
187
+ } finally {
188
+ noProfileSpawned.child.kill();
189
+ await new Promise(resolve => noProfileSpawned.child.once('exit', resolve));
190
+ }
191
+
192
+ // Case 7: third-party hook -> ready=false with HOOK_CONFLICT
193
+ r = await postJson('/api/projects/preflight-import', { localPath: repoThirdParty, aiProfileId: 'fake' });
194
+ assert.strictEqual(r.status, 200);
195
+ assert.strictEqual(r.body.ready, false);
196
+ assert(r.body.problems.some(p => p.code === 'HOOK_CONFLICT'));
197
+ assert.strictEqual(r.body.checks.hook.ok, false);
198
+ assert.strictEqual(r.body.checks.hook.reason, 'third-party');
199
+
200
+ // Case 8: duplicate detection after a successful import
201
+ r = await postJson('/api/projects/import', { localPath: repoOk, displayName: 'Dup', aiProfileId: 'fake' });
202
+ assert.strictEqual(r.status, 201, JSON.stringify(r.body));
203
+ const projectId = r.body.projectId;
204
+ r = await postJson('/api/projects/preflight-import', { localPath: repoOk, aiProfileId: 'fake' });
205
+ assert.strictEqual(r.status, 200);
206
+ assert.strictEqual(r.body.ready, false);
207
+ assert(r.body.problems.some(p => p.code === 'DUPLICATE_PROJECT' && p.existingProjectId === projectId));
208
+
209
+ // Case 9: invalid knowledgeLanguage -> problem reported
210
+ r = await postJson('/api/projects/preflight-import', { localPath: repoOk, aiProfileId: 'fake', knowledgeLanguage: 'xx-INVALID' });
211
+ assert.strictEqual(r.status, 200);
212
+ assert.strictEqual(r.body.ready, false);
213
+ assert(r.body.problems.some(p => p.code === 'KNOWLEDGE_LANGUAGE_INVALID'));
214
+
215
+ // Case 10: path missing entirely
216
+ r = await postJson('/api/projects/preflight-import', { localPath: path.join(dataDir, 'does-not-exist') });
217
+ assert.strictEqual(r.status, 200);
218
+ assert.strictEqual(r.body.ready, false);
219
+ assert(r.body.problems.some(p => p.code === 'PATH_MISSING'));
220
+ } finally {
221
+ spawned.child.kill();
222
+ await new Promise(resolve => spawned.child.once('exit', resolve));
223
+ }
224
+ } finally {
225
+ fs.rmSync(dataDir, { recursive: true, force: true });
226
+ for (const dir of [repoOk, repoEmpty, repoNonGit, repoSpaces, repoThirdParty]) {
227
+ try { fs.rmSync(dir, { recursive: true, force: true }); } catch {}
228
+ }
229
+ }
230
+ console.log('import-preflight-api-test PASS');
231
+ })().catch(error => {
232
+ console.error(error.stack || error.message);
233
+ process.exit(1);
234
+ });