@voxgig/sdkgen 4.2.8 → 4.4.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 (63) hide show
  1. package/bin/voxgig-sdkgen +1 -1
  2. package/dist/helpers/naming.d.ts +2 -1
  3. package/dist/helpers/naming.js +50 -12
  4. package/dist/helpers/naming.js.map +1 -1
  5. package/dist/sdkgen.d.ts +2 -2
  6. package/dist/sdkgen.js +4 -3
  7. package/dist/sdkgen.js.map +1 -1
  8. package/dist/tsconfig.tsbuildinfo +1 -1
  9. package/package.json +1 -1
  10. package/project/.sdk/src/cmp/zig/Config_zig.ts +1 -1
  11. package/project/.sdk/tm/c/tests/primary_corpus_test.c +565 -0
  12. package/project/.sdk/tm/c/utility/prepare_method.c +3 -1
  13. package/project/.sdk/tm/c/utility/transform_request.c +4 -3
  14. package/project/.sdk/tm/clojure/src/sdk/core.clj +55 -5
  15. package/project/.sdk/tm/clojure/test/sdk/test/primary.clj +171 -1
  16. package/project/.sdk/tm/clojure/test/sdk/test/struct_corpus.clj +13 -2
  17. package/project/.sdk/tm/clojure/test/sdk/test_runner.clj +2 -0
  18. package/project/.sdk/tm/csharp/test/CustomUtilityTest.cs +104 -0
  19. package/project/.sdk/tm/csharp/utility/MakeOptions.cs +82 -2
  20. package/project/.sdk/tm/elixir/lib/projectname/utility.ex +50 -2
  21. package/project/.sdk/tm/elixir/test/primary_utility_test.exs +18 -200
  22. package/project/.sdk/tm/elixir/test/support/struct_corpus.ex +413 -2
  23. package/project/.sdk/tm/go/test/custom_utility_test.go +103 -0
  24. package/project/.sdk/tm/go/test/feature_corpus_test.go +550 -0
  25. package/project/.sdk/tm/go/utility/make_options.go +7 -1
  26. package/project/.sdk/tm/go/utility/register.go +194 -0
  27. package/project/.sdk/tm/java/test/CustomUtilityTest.java +55 -0
  28. package/project/.sdk/tm/java/test/FeatureCorpusTest.java +463 -0
  29. package/project/.sdk/tm/java/utility/MakeOptions.java +11 -2
  30. package/project/.sdk/tm/java/utility/Register.java +91 -0
  31. package/project/.sdk/tm/js/test/feature/Corpus.test.js +285 -0
  32. package/project/.sdk/tm/kotlin/utility/MakeOptions.kt +56 -2
  33. package/project/.sdk/tm/lua/utility/make_options.lua +22 -2
  34. package/project/.sdk/tm/ocaml/Makefile +15 -4
  35. package/project/.sdk/tm/ocaml/sdk_runtime.ml +8 -1
  36. package/project/.sdk/tm/ocaml/test/corpus_runner.ml +185 -0
  37. package/project/.sdk/tm/ocaml/test/primary_utility_test.ml +238 -0
  38. package/project/.sdk/tm/ocaml/test/struct_corpus.ml +5 -160
  39. package/project/.sdk/tm/perl/t/feature_corpus.t +345 -0
  40. package/project/.sdk/tm/perl/utility/make_options.pm +33 -1
  41. package/project/.sdk/tm/php/core/Context.php +3 -0
  42. package/project/.sdk/tm/php/core/Control.php +13 -0
  43. package/project/.sdk/tm/php/core/Error.php +12 -0
  44. package/project/.sdk/tm/php/test/FeatureCorpusTest.php +376 -0
  45. package/project/.sdk/tm/php/utility/MakeOptions.php +30 -1
  46. package/project/.sdk/tm/py/pkg/utility/make_options.py +42 -1
  47. package/project/.sdk/tm/py/test/test_feature_corpus.py +309 -0
  48. package/project/.sdk/tm/rb/test/feature_corpus_test.rb +281 -0
  49. package/project/.sdk/tm/rb/utility/make_options.rb +32 -1
  50. package/project/.sdk/tm/scala/Makefile +1 -0
  51. package/project/.sdk/tm/scala/sdktest/PrimaryCorpus.scala +294 -0
  52. package/project/.sdk/tm/scala/sdktest/StructCorpus.scala +0 -0
  53. package/project/.sdk/tm/scala/utility/Make.scala +49 -2
  54. package/project/.sdk/tm/scala/utility/Prepare.scala +3 -1
  55. package/project/.sdk/tm/swift/Sources/ProjectNameSDK/utility/MakeOptions.swift +21 -3
  56. package/project/.sdk/tm/ts/src/feature/test/TestFeature.ts +13 -2
  57. package/project/.sdk/tm/ts/test/feature/Corpus.test.ts +287 -0
  58. package/project/.sdk/tm/zig/core/utility.zig +8 -1
  59. package/project/.sdk/tm/zig/test/primary_utility_test.zig +445 -0
  60. package/project/.sdk/tm/zig/test/struct_runner.zig +238 -0
  61. package/project/sdkgen-package.json +1 -1
  62. package/src/helpers/naming.ts +54 -12
  63. package/src/sdkgen.ts +2 -1
@@ -0,0 +1,287 @@
1
+
2
+ // Feature behaviour, driven by the SHARED corpus.
3
+ //
4
+ // This is the route PrimaryUtility.test.ts already takes for the utilities:
5
+ // language-neutral cases in .sdk/test/test.json, executed against the REAL
6
+ // generated SDK. Features here are ordinary classes in ordinary compiled
7
+ // source, unit-tested the ordinary way — no transpiled templates, and no
8
+ // miniature of the pipeline standing in for the pipeline (which is what
9
+ // harness.ts does, and why its assertions can only be as right as the
10
+ // miniature is). A feature is built through the generated config, wrapped
11
+ // into a client built by the generated constructor, and driven by a real
12
+ // entity operation. What is asserted is what ships.
13
+ //
14
+ // Everything in a case is data: features are activated by name, options are
15
+ // plain JSON, the transport is scripted by `res`, and the assertion is a
16
+ // subset of the client's own record. Turning `res` into a fetcher is the one
17
+ // piece each language writes for itself.
18
+
19
+ import { test, describe, before } from 'node:test'
20
+ import { ok, deepStrictEqual } from 'node:assert'
21
+
22
+ import { readFileSync } from 'node:fs'
23
+ import { join } from 'node:path'
24
+
25
+ import { SDK, TEST_JSON_FILE } from '../utility/index'
26
+
27
+
28
+ // Features with a corpus section. A name here with no section is a skip, not
29
+ // a failure: an SDK generated without the feature has nothing to run.
30
+ const FEATURES = ['cost']
31
+
32
+
33
+ // One operation this SDK can actually perform.
34
+ type OpRef = {
35
+ key: string // '<entity>.<op>' — how features attribute it
36
+ accessor: string // the client method returning the entity
37
+ entity: string
38
+ op: string
39
+ }
40
+
41
+
42
+ // A scripted transport built from a case's `res` list. Responses are consumed
43
+ // in order and the last one repeats, so a case that does not care how many
44
+ // attempts happen need only declare one.
45
+ function scriptedFetcher(res: any[]) {
46
+ let n = -1
47
+ return async function (_ctx: any, _url: string, _fetchdef: any) {
48
+ n++
49
+ const spec = res[n < res.length ? n : res.length - 1] || {}
50
+
51
+ if (true === spec.throw) {
52
+ throw new Error('scripted transport failure')
53
+ }
54
+
55
+ const headers: Record<string, any> = spec.headers || {}
56
+ const status = null == spec.status ? 200 : spec.status
57
+
58
+ return {
59
+ status,
60
+ statusText: status < 400 ? 'OK' : 'ERR',
61
+ body: 'not-used',
62
+ json: async () => (undefined === spec.body ? {} : spec.body),
63
+ headers: {
64
+ get(key: string) {
65
+ const lower = String(key).toLowerCase()
66
+ for (const k of Object.keys(headers)) {
67
+ if (k.toLowerCase() === lower) { return headers[k] }
68
+ }
69
+ return undefined
70
+ },
71
+ forEach(cb: any) { Object.keys(headers).forEach((k) => cb(headers[k], k, this)) },
72
+ },
73
+ }
74
+ }
75
+ }
76
+
77
+
78
+ function makeClient(kase: any): any {
79
+ return new (SDK as any)({
80
+ feature: kase.feature,
81
+ utility: { fetcher: scriptedFetcher(kase.res || [{ status: 200, body: {} }]) },
82
+ })
83
+ }
84
+
85
+
86
+ // Every operation this SDK declares, in a stable order.
87
+ //
88
+ // The corpus cannot name an entity — it is shared by SDKs that have none in
89
+ // common — so the runner finds them here. The generated client exposes one
90
+ // capitalised, zero-argument accessor per entity, and the entity it returns
91
+ // carries the same `name` the config is keyed by; that pairing is what turns
92
+ // a config entry back into a callable method.
93
+ function candidates(client: any): OpRef[] {
94
+ const entities: Record<string, any> = client._rootctx.config.entity || {}
95
+
96
+ const accessor: Record<string, string> = {}
97
+ for (const m of Object.getOwnPropertyNames(Object.getPrototypeOf(client))) {
98
+ if (!/^[A-Z]/.test(m) || 'function' !== typeof client[m]) { continue }
99
+ let inst: any
100
+ try { inst = client[m]() }
101
+ catch (e) { continue }
102
+ if (null != inst && 'string' === typeof inst.name && null != entities[inst.name]) {
103
+ accessor[inst.name] = m
104
+ }
105
+ }
106
+
107
+ const out: OpRef[] = []
108
+ for (const entity of Object.keys(entities).sort()) {
109
+ if (null == accessor[entity]) { continue }
110
+ for (const op of Object.keys(entities[entity].op || {}).sort()) {
111
+ out.push({ key: entity + '.' + op, accessor: accessor[entity], entity, op })
112
+ }
113
+ }
114
+ return out
115
+ }
116
+
117
+
118
+ // Pick operations the corpus can drive, by DRIVING them: an op is usable when
119
+ // it completes against a plain 200 with no feature active. Declared ops are
120
+ // not all callable with no arguments (a required path parameter, a body), and
121
+ // a case that failed for that reason would look like a feature defect.
122
+ async function usableOps(want: number): Promise<OpRef[]> {
123
+ const picked: OpRef[] = []
124
+ for (const cand of candidates(makeClient({}))) {
125
+ const client = makeClient({})
126
+ try {
127
+ await client[cand.accessor]()[cand.op]({}, {})
128
+ }
129
+ catch (e) { continue }
130
+ picked.push(cand)
131
+ if (want <= picked.length) { break }
132
+ }
133
+ return picked
134
+ }
135
+
136
+
137
+ // Replace #OP1/#OP2 throughout a case, keys included.
138
+ function resolve(node: any, tokens: Record<string, string>): any {
139
+ if ('string' === typeof node) {
140
+ let s = node
141
+ for (const t of Object.keys(tokens)) { s = s.split(t).join(tokens[t]) }
142
+ return s
143
+ }
144
+ if (Array.isArray(node)) {
145
+ return node.map((n) => resolve(n, tokens))
146
+ }
147
+ if (null != node && 'object' === typeof node) {
148
+ const out: any = {}
149
+ for (const k of Object.keys(node)) {
150
+ out[resolve(k, tokens)] = resolve(node[k], tokens)
151
+ }
152
+ return out
153
+ }
154
+ return node
155
+ }
156
+
157
+
158
+ // Which #OPn tokens a case uses. A case wanting more operations than this SDK
159
+ // has is skipped rather than failed.
160
+ function tokensUsed(kase: any): number {
161
+ const m = JSON.stringify(kase).match(/#OP(\d+)/g) || []
162
+ return m.reduce((max, t) => Math.max(max, Number(t.slice(3))), 0)
163
+ }
164
+
165
+
166
+ // Assert that `actual` contains `expect`, recursively. Cases assert only the
167
+ // fields they are about, so a full deepStrictEqual would force every case to
168
+ // restate the whole record.
169
+ function subset(actual: any, expect: any, path: string) {
170
+ if (null != expect && 'object' === typeof expect && !Array.isArray(expect)) {
171
+ for (const k of Object.keys(expect)) {
172
+ ok(null != actual, `${path}.${k}: nothing at ${path}`)
173
+ subset(actual[k], expect[k], `${path}.${k}`)
174
+ }
175
+ return
176
+ }
177
+ deepStrictEqual(actual, expect, path)
178
+ }
179
+
180
+
181
+ describe('FeatureCorpus', () => {
182
+
183
+ let corpus: any
184
+ let ops: OpRef[] = []
185
+ let byKey: Record<string, OpRef> = {}
186
+
187
+
188
+ before(async () => {
189
+ corpus = JSON.parse(readFileSync(join(__dirname, '..', TEST_JSON_FILE), 'utf8'))
190
+ ops = await usableOps(2)
191
+ byKey = {}
192
+ for (const o of ops) { byKey[o.key] = o }
193
+ })
194
+
195
+
196
+ // A corpus with no `feature` section is a SKIP, not a failure.
197
+ //
198
+ // Each project carries its OWN materialised copy of .sdk/test/test.json, so
199
+ // a project scaffolded before the section existed legitimately has no cases
200
+ // to run - and a hard assertion here turned that into a red suite in every
201
+ // SDK on the fleet, for a corpus the project had simply not re-pulled yet.
202
+ //
203
+ // The strict check belongs where the corpus is CONTROLLED, not where it is
204
+ // consumed: sdkgen's own end-to-end lane generates against a corpus it
205
+ // supplies and requires the cases to actually run, so a section that goes
206
+ // missing there still fails loudly.
207
+ test('the corpus carries a feature section', (t) => {
208
+ if (null == corpus.feature) {
209
+ return t.skip(
210
+ 'this project\'s test.json has no `feature` section - recompile the ' +
211
+ 'corpus (create-sdkgen .sdk/test/feature/) to run these cases')
212
+ }
213
+ })
214
+
215
+
216
+ // At least one operation, or every case below would skip and the whole
217
+ // suite would report green having run nothing.
218
+ test('this SDK has an operation the corpus can drive', () => {
219
+ ok(0 < ops.length,
220
+ 'no declared operation completed against a plain 200 — the corpus ' +
221
+ 'cannot exercise a feature without one')
222
+ })
223
+
224
+
225
+ for (const name of FEATURES) {
226
+
227
+ test(name, async (t) => {
228
+ const section = corpus.feature?.[name]
229
+ if (null == section) {
230
+ return t.skip(`no corpus section for ${name}`)
231
+ }
232
+
233
+ const probe: any = makeClient({})
234
+ if (!probe._rootctx.config.hasFeature(name)) {
235
+ return t.skip(`this SDK was generated without the ${name} feature`)
236
+ }
237
+
238
+ const cases: any[] = section.basic?.set || []
239
+ ok(0 < cases.length,
240
+ `corpus section feature.${name} ran ZERO cases — a renamed section ` +
241
+ `or an emptied fixture must fail loudly, not pass silently`)
242
+
243
+ let ran = 0
244
+ for (const raw of cases) {
245
+ const need = tokensUsed(raw)
246
+ if (ops.length < need) {
247
+ t.diagnostic(`skip "${raw.name}": needs ${need} operations, this SDK offers ${ops.length}`)
248
+ continue
249
+ }
250
+
251
+ const tokens: Record<string, string> = {}
252
+ for (let i = 0; i < need; i++) { tokens['#OP' + (i + 1)] = ops[i].key }
253
+
254
+ const kase = resolve(raw, tokens)
255
+ const client = makeClient(kase)
256
+
257
+ for (const step of (kase.op || [])) {
258
+ const ref = byKey[step.op]
259
+ ok(null != ref, `${kase.name}: no operation ${step.op}`)
260
+ try {
261
+ await client[ref.accessor]()[ref.op]({}, step.ctrl || {})
262
+ ok(null == step.err,
263
+ `${kase.name}: ${step.op} was expected to fail, and did not`)
264
+ }
265
+ catch (err: any) {
266
+ if (null == step.err) { throw err }
267
+ if ('string' === typeof step.err) {
268
+ deepStrictEqual(err.code, step.err, `${kase.name}: wrong error code`)
269
+ }
270
+ }
271
+ }
272
+
273
+ subset(client[`_${name}`], kase.out, `${kase.name}: _${name}`)
274
+ ran++
275
+ }
276
+
277
+ ok(0 < ran, `every feature.${name} case was skipped`)
278
+ // Say how many ran. A partial run is legitimate (an SDK with one
279
+ // operation skips the cases needing two) but it should be visible
280
+ // rather than inferred from a green tick - and it is the one line
281
+ // sdkgen's end-to-end lane reads, in the same wording, from every
282
+ // language's runner.
283
+ t.diagnostic(`feature.${name}: ran ${ran} of ${cases.length} ` +
284
+ `case(s) against ${ops.length} operation(s)`)
285
+ })
286
+ }
287
+ })
@@ -1019,7 +1019,14 @@ pub fn prepare_method_util(ctx: *Context) []const u8 {
1019
1019
  if (std.mem.eql(u8, opname, "list")) return "GET";
1020
1020
  if (std.mem.eql(u8, opname, "remove")) return "DELETE";
1021
1021
  if (std.mem.eql(u8, opname, "patch")) return "PATCH";
1022
- return "GET";
1022
+
1023
+ // NO CATCH-ALL GET. The ts reference returns methodMap[key], which is
1024
+ // undefined for an op the map does not name, so the request is refused
1025
+ // rather than issued. Returning "GET" here made every unrecognised op a
1026
+ // GET — a mistyped or unsupported op quietly fetched — and the method
1027
+ // feeds the allow.method gate, so it is not cosmetic. ocaml had the same
1028
+ // fallback; the shared corpus caught both.
1029
+ return "";
1023
1030
  }
1024
1031
 
1025
1032
  pub fn prepare_headers_util(ctx: *Context) Value {