dsh-unreal-python-api 0.0.0-stage → 1.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.
package/test/smoke.mjs ADDED
@@ -0,0 +1,222 @@
1
+ /**
2
+ * Exercises the plugin's data path against the live documentation, without the
3
+ * DSH runtime: download and parse the inventory, search it, list members, read
4
+ * a page's syntax, and build a scaffold from verified symbols.
5
+ *
6
+ * Run: node test/smoke.mjs
7
+ */
8
+
9
+ import assert from 'node:assert/strict'
10
+ import { listChildren, loadInventory, pageUrl, resolveCacheDir, searchSymbols, summarize } from '../lib/inventory.js'
11
+ import { readSymbolDoc } from '../lib/docs.js'
12
+ import { buildToolset } from '../lib/builder.js'
13
+ import { execFileSync } from 'node:child_process'
14
+ import { mkdtempSync, writeFileSync } from 'node:fs'
15
+ import { tmpdir } from 'node:os'
16
+ import { join } from 'node:path'
17
+
18
+ /** Parse a generated artifact with its own language's compiler. */
19
+ function assertParses(label, text, extension) {
20
+ if (text === '') return
21
+ const file = join(mkdtempSync(join(tmpdir(), 'unreal-python-api-')), `artifact.${extension}`)
22
+ writeFileSync(file, text)
23
+ if (extension === 'mjs') {
24
+ execFileSync(process.execPath, ['--check', file], { stdio: 'pipe' })
25
+ } else {
26
+ try {
27
+ execFileSync('python', ['-c', `import ast, pathlib; ast.parse(pathlib.Path(${JSON.stringify(file)}).read_text(encoding="utf-8"))`], { stdio: 'pipe' })
28
+ } catch (error) {
29
+ if (error.code === 'ENOENT') {
30
+ console.log(` ${label}: python is not on PATH, parse check skipped`)
31
+ return
32
+ }
33
+ throw error
34
+ }
35
+ }
36
+ console.log(` ${label}: parses as ${extension}`)
37
+ }
38
+
39
+ const DOC_BASE_URL = 'https://dev.epicgames.com/documentation/en-us/unreal-engine/python-api/'
40
+ const cacheDir = resolveCacheDir()
41
+ console.log(`cache dir: ${cacheDir}`)
42
+
43
+ const startedAt = Date.now()
44
+ const loaded = await loadInventory({ docBaseUrl: DOC_BASE_URL, cacheDir, refreshHours: 24, timeoutMs: 60_000 })
45
+ const { inventory, version, source } = loaded
46
+ console.log(`inventory: version=${version} symbols=${inventory.names.length} source=${source} in ${Date.now() - startedAt}ms`)
47
+ console.log(`inventory url: ${loaded.url}`)
48
+ const counts = summarize(inventory).map((entry) => `${entry.kind}=${entry.count}`).join(' ')
49
+ console.log(`kinds: ${counts}`)
50
+
51
+ assert.equal(version, '5.8', 'published inventory reports 5.8')
52
+ assert.ok(inventory.names.length > 60_000, `expected the full inventory, saw ${inventory.names.length}`)
53
+ assert.ok(summarize(inventory).some((entry) => entry.kind === 'class'), 'inventory carries classes')
54
+
55
+ const search = searchSymbols(inventory, { query: 'set_actor_scale3d', limit: 5 })
56
+ console.log(`\nsearch "set_actor_scale3d": ${search.total} matches`)
57
+ for (const match of search.matches) console.log(` ${match.name} [${match.kind}] ${match.url}`)
58
+ assert.ok(search.matches.some((match) => match.name === 'unreal.Actor.set_actor_scale3d'), 'exact member found')
59
+ assert.ok(search.matches[0].name === 'unreal.Actor.set_actor_scale3d', 'exact match ranks first')
60
+ assert.ok(search.matches[0].url.endsWith('#unreal.Actor.set_actor_scale3d'), `url points at the member anchor: ${search.matches[0].url}`)
61
+
62
+ const broad = searchSymbols(inventory, { query: 'save_asset', kind: 'method', limit: 5 })
63
+ console.log(`\nsearch "save_asset" (methods only): ${broad.total} matches, first=${broad.matches[0]?.name}`)
64
+ assert.ok(broad.total > 0, 'found documented save_asset methods')
65
+ assert.ok(broad.matches.every((match) => match.kind === 'method'), 'kind filter holds')
66
+
67
+ const members = listChildren(inventory, { parent: 'unreal.EditorAssetLibrary', limit: 8 })
68
+ console.log(`\nunreal.EditorAssetLibrary: ${members.total} direct members (${members.counts.map((entry) => `${entry.kind}=${entry.count}`).join(' ')})`)
69
+ for (const row of members.rows) console.log(` ${row.name} [${row.kind}]`)
70
+ assert.ok(members.total > 20, 'EditorAssetLibrary exposes members')
71
+ assert.ok(members.rows.every((row) => !row.name.slice('unreal.EditorAssetLibrary.'.length).includes('.')), 'direct members only')
72
+
73
+ const position = inventory.index.get('unreal.Actor.set_actor_scale3d')
74
+ assert.ok(position !== undefined, 'inventory indexes the symbol for page lookup')
75
+ const syntax = await readSymbolDoc({
76
+ docBaseUrl: DOC_BASE_URL,
77
+ symbol: 'unreal.Actor.set_actor_scale3d',
78
+ uri: inventory.uris[position],
79
+ kind: 'method',
80
+ timeoutMs: 60_000,
81
+ })
82
+ console.log(`\nsyntax ${syntax.symbol} (resolved=${syntax.resolved})`)
83
+ console.log(` signature: ${syntax.signature}`)
84
+ console.log(` summary: ${syntax.description}`)
85
+ for (const parameter of syntax.parameters) console.log(` param: ${parameter.name} - ${parameter.text}`)
86
+ console.log(` returns: ${syntax.returns}`)
87
+ console.log(` url: ${syntax.url}`)
88
+ assert.equal(syntax.resolved, true, 'page carries an anchor for the symbol')
89
+ assert.ok(syntax.signature.includes('set_actor_scale3d'), 'signature names the symbol')
90
+ assert.ok(syntax.url.startsWith(DOC_BASE_URL), 'url is absolute')
91
+ assert.ok(syntax.url.endsWith('#unreal.Actor.set_actor_scale3d'), `syntax url is anchored to the symbol: ${syntax.url}`)
92
+
93
+ const missing = await readSymbolDoc({
94
+ docBaseUrl: DOC_BASE_URL,
95
+ symbol: 'unreal.Nothing.here',
96
+ uri: inventory.uris[position],
97
+ kind: 'method',
98
+ timeoutMs: 60_000,
99
+ })
100
+ assert.equal(missing.resolved, false, 'an absent symbol is reported unresolved, never invented')
101
+ console.log(`\nabsent symbol reported unresolved: ${missing.note}`)
102
+
103
+ const resolveSymbol = (symbol) => {
104
+ const at = inventory.index.get(symbol)
105
+ if (at === undefined) return { found: false }
106
+ return { found: true, kind: inventory.kinds[at].replace('py:', ''), url: pageUrl(DOC_BASE_URL, inventory.uris[at], symbol) }
107
+ }
108
+
109
+ const built = buildToolset(
110
+ {
111
+ toolsetName: 'SmokeTestTools',
112
+ description: 'Smoke-test toolset for the Unreal Python API plugin.',
113
+ tools: [
114
+ {
115
+ name: 'read_actor_scale',
116
+ description: 'Reads an actor scale through the documented API.',
117
+ parameters: [{ name: 'actor', type: 'unreal.Actor', description: 'Actor to read.' }],
118
+ returns: 'unreal.Vector',
119
+ symbols: ['unreal.Actor.get_actor_scale3d', 'unreal.Actor.get_actor_scale3d_nonexistent'],
120
+ },
121
+ {
122
+ name: 'save_dirty',
123
+ description: 'Saves every dirty package.',
124
+ symbols: ['unreal.EditorAssetLibrary.save_asset'],
125
+ },
126
+ ],
127
+ },
128
+ resolveSymbol,
129
+ )
130
+
131
+ const frozen = buildToolset(
132
+ Object.freeze({
133
+ toolsetName: 'FrozenArgs',
134
+ description: 'Regression: tool arguments arrive non-extensible.',
135
+ tools: Object.freeze([
136
+ Object.freeze({
137
+ name: 'read_scale',
138
+ description: 'Reads an actor scale.',
139
+ parameters: Object.freeze([Object.freeze({ name: 'actor', type: 'unreal.Actor', description: 'Actor to read.' })]),
140
+ returns: 'unreal.Vector',
141
+ symbols: Object.freeze(['unreal.Actor.get_actor_scale3d']),
142
+ }),
143
+ ]),
144
+ }),
145
+ resolveSymbol,
146
+ )
147
+ assert.ok(
148
+ frozen.code.includes('def read_scale(actor: unreal.Actor) -> unreal.Vector:'),
149
+ 'frozen tool arguments build without mutating the caller input',
150
+ )
151
+ console.log('frozen-argument regression: ok')
152
+
153
+ console.log(`\nscaffold: verified=${built.verified.length} unresolved=${built.unresolved.length}`)
154
+ for (const note of built.notes) console.log(` note: ${note}`)
155
+ assert.equal(built.unresolved.length, 1, 'the invented symbol is reported, not written')
156
+ assert.ok(built.unresolved[0].startsWith('unreal.Actor.get_actor_scale3d_nonexistent'), 'the invented symbol is named')
157
+ assert.ok(built.code.includes('class SmokeTestTools(unreal.ToolsetDefinition)'), 'toolset class emitted')
158
+ assert.ok(built.code.includes('@toolset_registry.tool_call'), 'tools are decorated')
159
+ assert.ok(built.code.includes('def read_actor_scale(actor: unreal.Actor) -> unreal.Vector:'), 'annotated signature emitted')
160
+ assert.ok(built.code.includes('unreal.Actor.get_actor_scale3d https://'), 'verified symbol recorded with its page')
161
+ assert.ok(built.code.includes('#unreal.Actor.get_actor_scale3d'), 'verified symbol deep-links to its anchor')
162
+ assert.ok(!built.code.includes('nonexistent'), 'unresolved symbol kept out of the code')
163
+ assert.ok(built.code.includes('register_toolset_class'), 'registration emitted')
164
+ assert.ok(built.testCode.includes('class SmokeTestToolsTestCase(ToolCallTestCase)'), 'test skeleton emitted')
165
+
166
+ // --- the two market-facing styles: MCP descriptors and a DSH tool module -------
167
+ const builderInput = (style) => ({
168
+ toolsetName: 'TerritoryTools',
169
+ description: 'Manage TerritoryFramework territory state from the editor.',
170
+ style,
171
+ testModule: 'territory_tools',
172
+ tools: [
173
+ {
174
+ name: 'save_dirty_assets',
175
+ description: 'Save every dirty asset in the project.',
176
+ parameters: [{ name: 'only_if_is_dirty', type: 'bool', description: 'Skip clean assets.' }],
177
+ returns: 'bool',
178
+ symbols: ['unreal.EditorAssetLibrary.save_asset', 'unreal.NotARealSymbol'],
179
+ },
180
+ ],
181
+ })
182
+
183
+ const mcp = buildToolset(builderInput('mcp_tool'), resolveSymbol)
184
+ assert.equal(mcp.style, 'mcp_tool')
185
+ const descriptor = JSON.parse(mcp.code)
186
+ assert.equal(descriptor.tools.length, 1)
187
+ assert.equal(descriptor.tools[0].name, 'save_dirty_assets')
188
+ assert.equal(descriptor.tools[0].inputSchema.additionalProperties, false)
189
+ assert.deepEqual(descriptor.tools[0].inputSchema.required, ['only_if_is_dirty'])
190
+ assert.equal(descriptor.tools[0].inputSchema.properties.only_if_is_dirty.type, 'boolean')
191
+ assert.ok(!mcp.code.includes('NotARealSymbol'), 'unverified symbol stays out of the descriptor')
192
+ assert.ok(mcp.testCode.includes('TOOL_SPEC = json.loads'), 'handler module carries the descriptors')
193
+ assert.ok(mcp.testCode.includes('raise NotImplementedError'), 'handler body is unimplemented')
194
+
195
+ const dsh = buildToolset(builderInput('dsh_tool'), resolveSymbol)
196
+ assert.equal(dsh.style, 'dsh_tool')
197
+ assert.ok(dsh.code.includes("from '@deepseek-ai/dsh-tools'"), 'DSH module imports defineTool')
198
+ assert.ok(dsh.code.includes('additionalProperties: false'), 'DSH output schema is closed')
199
+ assert.ok(dsh.code.includes('export function registerTools(ctx)'), 'DSH module registers through ctx.tools')
200
+ assert.ok(!dsh.code.includes('NotARealSymbol'), 'unverified symbol stays out of the DSH module')
201
+ assert.ok(dsh.testCode.includes('registerTools'), 'DSH test exercises registration')
202
+
203
+ assert.throws(() => buildToolset(builderInput('nope'), resolveSymbol), /unsupported style/)
204
+
205
+ // --- every generated artifact must parse in its own language -------------------
206
+ console.log('parse checks:')
207
+ assertParses('toolset module', built.code, 'py')
208
+ assertParses('toolset test module', built.testCode, 'py')
209
+ assertParses('mcp handler module', mcp.testCode, 'py')
210
+ assertParses('dsh tool module', dsh.code, 'mjs')
211
+ assertParses('dsh test module', dsh.testCode, 'mjs')
212
+ console.log('extra styles: mcp_tool + dsh_tool OK')
213
+ console.log('\n--- generated MCP descriptors ---')
214
+ console.log(mcp.code)
215
+ console.log('--- generated DSH tool module ---')
216
+ console.log(dsh.code)
217
+
218
+ console.log('\n--- generated toolset code ---')
219
+ console.log(built.code)
220
+ console.log('--- generated test code ---')
221
+ console.log(built.testCode)
222
+ console.log('\nSMOKE OK')