@aloma.io/integration-sdk 3.8.67 → 3.8.69

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 (65) hide show
  1. package/README.md +60 -0
  2. package/build/testing/fake-aloma.d.mts +87 -0
  3. package/build/testing/fake-aloma.mjs +248 -0
  4. package/build/testing/harness.d.mts +51 -0
  5. package/build/testing/harness.mjs +139 -0
  6. package/build/testing/index.d.mts +15 -0
  7. package/build/testing/index.mjs +13 -0
  8. package/build/testing/keys.d.mts +9 -0
  9. package/build/testing/keys.mjs +15 -0
  10. package/build/testing/network-guard.d.mts +3 -0
  11. package/build/testing/network-guard.mjs +49 -0
  12. package/build/testing/runtime-connector.d.mts +11 -0
  13. package/build/testing/runtime-connector.mjs +38 -0
  14. package/build/testing/vendor-mock.d.mts +59 -0
  15. package/build/testing/vendor-mock.mjs +121 -0
  16. package/build/transform/index.d.mts +2 -2
  17. package/build/transform/index.mjs +493 -78
  18. package/package.json +8 -4
  19. package/src/testing/fake-aloma.mts +306 -0
  20. package/src/testing/harness.mts +172 -0
  21. package/src/testing/index.mts +15 -0
  22. package/src/testing/keys.mts +19 -0
  23. package/src/testing/network-guard.mts +57 -0
  24. package/src/testing/runtime-connector.mts +50 -0
  25. package/src/testing/vendor-mock.mts +166 -0
  26. package/src/transform/index.mts +530 -96
  27. package/test/MUTATION-entry.md +21 -0
  28. package/test/MUTATION.md +43 -0
  29. package/test/fixtures/transform/basic/expected.json +9 -0
  30. package/test/fixtures/transform/basic/src/controller/abstract.mts +5 -0
  31. package/test/fixtures/transform/basic/src/controller/index.mts +30 -0
  32. package/test/fixtures/transform/basic/tsconfig.json +1 -0
  33. package/test/fixtures/transform/interface-param/src/controller/abstract.mts +5 -0
  34. package/test/fixtures/transform/interface-param/src/controller/index.mts +12 -0
  35. package/test/fixtures/transform/interface-param/tsconfig.json +1 -0
  36. package/test/fixtures/transform/overloads/expected.json +6 -0
  37. package/test/fixtures/transform/overloads/src/controller/abstract.mts +5 -0
  38. package/test/fixtures/transform/overloads/src/controller/index.mts +11 -0
  39. package/test/fixtures/transform/overloads/tsconfig.json +1 -0
  40. package/test/fixtures/transform/tdd-stub/expected.json +8 -0
  41. package/test/fixtures/transform/tdd-stub/src/controller/abstract.mts +6 -0
  42. package/test/fixtures/transform/tdd-stub/src/controller/index.mts +204 -0
  43. package/test/fixtures/transform/tdd-stub/tsconfig.json +1 -0
  44. package/test/fixtures/transform/two-exports/src/controller/abstract.mts +5 -0
  45. package/test/fixtures/transform/two-exports/src/controller/index.mts +9 -0
  46. package/test/fixtures/transform/two-exports/tsconfig.json +1 -0
  47. package/test/fixtures/transform/type-error/src/controller/abstract.mts +5 -0
  48. package/test/fixtures/transform/type-error/src/controller/index.mts +7 -0
  49. package/test/fixtures/transform/type-error/tsconfig.json +1 -0
  50. package/test/fixtures/transform/typed/expected.json +8 -0
  51. package/test/fixtures/transform/typed/src/controller/abstract.mts +5 -0
  52. package/test/fixtures/transform/typed/src/controller/index.mts +13 -0
  53. package/test/fixtures/transform/typed/tsconfig.json +1 -0
  54. package/test/fixtures/transform/visibility/expected.json +9 -0
  55. package/test/fixtures/transform/visibility/src/controller/abstract.mts +5 -0
  56. package/test/fixtures/transform/visibility/src/controller/index.mts +19 -0
  57. package/test/fixtures/transform/visibility/tsconfig.json +1 -0
  58. package/test/harness-connector-smoke.test.mjs +72 -0
  59. package/test/harness.test.mjs +375 -0
  60. package/test/openapi-to-connector.test.mts +25 -7
  61. package/test/scenarios.test.mts +15 -7
  62. package/test/transform.test.mjs +68 -0
  63. package/tools/mutation-run-entry.mjs +52 -0
  64. package/tools/mutation-run.mjs +120 -0
  65. package/tools/run-tests.mjs +64 -0
@@ -0,0 +1,49 @@
1
+ import net from 'node:net';
2
+ /**
3
+ * Test-only network guard.
4
+ *
5
+ * While installed, any TCP connection to a host that is not loopback throws
6
+ * immediately. fetch (undici), http, https, ws and axios all end up in
7
+ * net.Socket.prototype.connect, so this is the safety net behind the vendor mock:
8
+ * even code that bypasses global fetch cannot reach the network from a test.
9
+ */
10
+ const LOOPBACK = new Set(['127.0.0.1', 'localhost', '::1', '[::1]']);
11
+ let original;
12
+ let installs = 0;
13
+ export const blocked = [];
14
+ const targetOf = (args) => {
15
+ // net.connect hands over a normalized [options, callback] array
16
+ const first = Array.isArray(args[0]) ? args[0][0] : args[0];
17
+ if (first && typeof first === 'object' && !Array.isArray(first)) {
18
+ if (first.path)
19
+ return { host: String(first.path), local: true };
20
+ return { host: String(first.host ?? 'localhost'), local: false };
21
+ }
22
+ if (typeof first === 'number' || (typeof first === 'string' && !Number.isNaN(Number(first)))) {
23
+ return { host: typeof args[1] === 'string' ? args[1] : 'localhost', local: false };
24
+ }
25
+ // unix socket path
26
+ return { host: String(first), local: true };
27
+ };
28
+ export const installNetworkGuard = () => {
29
+ if (installs++ > 0)
30
+ return;
31
+ original = net.Socket.prototype.connect;
32
+ net.Socket.prototype.connect = function (...args) {
33
+ const { host, local } = targetOf(args);
34
+ if (!local && !LOOPBACK.has(host)) {
35
+ const message = `NETWORK BLOCKED by connector test harness: connection to ${host} is not allowed in tests`;
36
+ blocked.push(host);
37
+ throw new Error(message);
38
+ }
39
+ return original.apply(this, args);
40
+ };
41
+ };
42
+ export const uninstallNetworkGuard = () => {
43
+ if (installs === 0)
44
+ return;
45
+ if (--installs > 0)
46
+ return;
47
+ net.Socket.prototype.connect = original;
48
+ original = undefined;
49
+ };
@@ -0,0 +1,11 @@
1
+ import type RuntimeContext from '../builder/runtime-context.mjs';
2
+ import { Connector } from '../internal/connector/index.mjs';
3
+ /**
4
+ * Turn what a connector repo builds (`await new Builder()...build()` gives a RuntimeContext) into a configured
5
+ * `Connector` that the ConnectorHarness can run, without calling `connector.run()` (which starts a metrics
6
+ * server on a fixed port and installs process signal handlers).
7
+ *
8
+ * This repeats the wiring of RuntimeContext.start(): methods become resolvers, config/oauth/endpoint are applied,
9
+ * main() starts the controller and sends a health check. If RuntimeContext.start() changes, change this too.
10
+ */
11
+ export declare const connectorFromRuntimeContext: (context: RuntimeContext) => Connector;
@@ -0,0 +1,38 @@
1
+ import { AbstractController } from '../controller/index.mjs';
2
+ import { buildResolvers } from '../builder/runtime-context.mjs';
3
+ import { Connector } from '../internal/connector/index.mjs';
4
+ /**
5
+ * Turn what a connector repo builds (`await new Builder()...build()` gives a RuntimeContext) into a configured
6
+ * `Connector` that the ConnectorHarness can run, without calling `connector.run()` (which starts a metrics
7
+ * server on a fixed port and installs process signal handlers).
8
+ *
9
+ * This repeats the wiring of RuntimeContext.start(): methods become resolvers, config/oauth/endpoint are applied,
10
+ * main() starts the controller and sends a health check. If RuntimeContext.start() changes, change this too.
11
+ */
12
+ export const connectorFromRuntimeContext = (context) => {
13
+ const { controller, data } = context;
14
+ if (!(controller instanceof AbstractController)) {
15
+ throw new Error('the controller needs to extend AbstractController');
16
+ }
17
+ const connector = new Connector({
18
+ id: data.id,
19
+ version: data.version,
20
+ name: `${data.id}/${data.version}`,
21
+ icon: undefined,
22
+ });
23
+ const configuration = connector.configure().config(data.config || {});
24
+ const methods = [...data.methods, '__autocomplete', '__endpoint', '__default'];
25
+ configuration.types(data.types).resolvers(buildResolvers(methods, controller));
26
+ if (data.options?.endpoint?.enabled) {
27
+ configuration.endpoint((arg) => controller.__endpoint(arg), data.options?.endpoint?.required);
28
+ }
29
+ if (data.auth?.oauth) {
30
+ configuration.oauth(data.auth?.oauth);
31
+ }
32
+ configuration.main(async ({ newTask, updateTask, config, oauth, getClient, getBlob, getBlobContent, createBlob, healthCheck }) => {
33
+ await controller._doStop();
34
+ await controller._doStart(config, oauth, newTask, updateTask, getClient, getBlob, getBlobContent, createBlob);
35
+ await healthCheck(controller);
36
+ });
37
+ return connector;
38
+ };
@@ -0,0 +1,59 @@
1
+ /// <reference types="node" resolution-mode="require"/>
2
+ export type VendorRequest = {
3
+ method: string;
4
+ url: string;
5
+ /** the url exactly as the connector passed it to fetch */
6
+ rawUrl: string;
7
+ headers: Record<string, string>;
8
+ body?: string;
9
+ };
10
+ export type VendorResponse = {
11
+ status?: number;
12
+ headers?: Record<string, string>;
13
+ /** sent as JSON with a json content-type */
14
+ json?: unknown;
15
+ /** sent as is */
16
+ text?: string;
17
+ };
18
+ export type VendorResponder = VendorResponse | ((request: VendorRequest) => VendorResponse | Promise<VendorResponse>);
19
+ export type UrlMatcher = string | RegExp | ((url: string) => boolean);
20
+ /**
21
+ * Mock for the HTTP calls a connector makes to its vendor API.
22
+ *
23
+ * - intercepts global fetch (what the SDK Fetcher and OAuth code use)
24
+ * - answers from routes you register with on()/get()/post()...
25
+ * - any request without a route is recorded in `unmocked` and rejected,
26
+ * verify() then fails the test even if the connector swallowed the error
27
+ * - also installs the network guard, so nothing can reach a non-loopback host
28
+ */
29
+ export declare class VendorMock {
30
+ readonly requests: VendorRequest[];
31
+ readonly unmocked: VendorRequest[];
32
+ private routes;
33
+ private originalFetch?;
34
+ private passthrough;
35
+ /** urls for which the real fetch is used (the fake ALOMA endpoint); everything else is mocked */
36
+ allowPassthrough(fn: (url: URL) => boolean): this;
37
+ on(method: string, matcher: UrlMatcher, respond: VendorResponder, options?: {
38
+ times?: number;
39
+ }): this;
40
+ get(matcher: UrlMatcher, respond: VendorResponder, options?: {
41
+ times?: number;
42
+ }): this;
43
+ post(matcher: UrlMatcher, respond: VendorResponder, options?: {
44
+ times?: number;
45
+ }): this;
46
+ put(matcher: UrlMatcher, respond: VendorResponder, options?: {
47
+ times?: number;
48
+ }): this;
49
+ delete(matcher: UrlMatcher, respond: VendorResponder, options?: {
50
+ times?: number;
51
+ }): this;
52
+ install(): this;
53
+ uninstall(): void;
54
+ /** throws if the connector made a request that had no mock, even if it caught the error */
55
+ verify(): void;
56
+ /** throws if a registered route was never used */
57
+ verifyAllRoutesUsed(): void;
58
+ private handle;
59
+ }
@@ -0,0 +1,121 @@
1
+ import { installNetworkGuard, uninstallNetworkGuard } from './network-guard.mjs';
2
+ // "https:/host/x" (one slash, which the SDK Fetcher produces) and "https://host/x" are the same URL to fetch
3
+ const canonical = (url) => {
4
+ try {
5
+ return new URL(url).href;
6
+ }
7
+ catch (e) {
8
+ return url;
9
+ }
10
+ };
11
+ const matches = (matcher, url) => {
12
+ if (typeof matcher === 'string')
13
+ return canonical(matcher) === url;
14
+ if (matcher instanceof RegExp)
15
+ return matcher.test(url);
16
+ return matcher(url);
17
+ };
18
+ /**
19
+ * Mock for the HTTP calls a connector makes to its vendor API.
20
+ *
21
+ * - intercepts global fetch (what the SDK Fetcher and OAuth code use)
22
+ * - answers from routes you register with on()/get()/post()...
23
+ * - any request without a route is recorded in `unmocked` and rejected,
24
+ * verify() then fails the test even if the connector swallowed the error
25
+ * - also installs the network guard, so nothing can reach a non-loopback host
26
+ */
27
+ export class VendorMock {
28
+ requests = [];
29
+ unmocked = [];
30
+ routes = [];
31
+ originalFetch;
32
+ passthrough = () => false;
33
+ /** urls for which the real fetch is used (the fake ALOMA endpoint); everything else is mocked */
34
+ allowPassthrough(fn) {
35
+ this.passthrough = fn;
36
+ return this;
37
+ }
38
+ on(method, matcher, respond, options = {}) {
39
+ this.routes.push({ method: method.toUpperCase(), matcher, respond, times: options.times ?? Infinity, used: 0 });
40
+ return this;
41
+ }
42
+ get(matcher, respond, options) {
43
+ return this.on('GET', matcher, respond, options);
44
+ }
45
+ post(matcher, respond, options) {
46
+ return this.on('POST', matcher, respond, options);
47
+ }
48
+ put(matcher, respond, options) {
49
+ return this.on('PUT', matcher, respond, options);
50
+ }
51
+ delete(matcher, respond, options) {
52
+ return this.on('DELETE', matcher, respond, options);
53
+ }
54
+ install() {
55
+ if (this.originalFetch)
56
+ return this;
57
+ installNetworkGuard();
58
+ this.originalFetch = globalThis.fetch;
59
+ globalThis.fetch = ((input, init) => this.handle(input, init));
60
+ return this;
61
+ }
62
+ uninstall() {
63
+ if (!this.originalFetch)
64
+ return;
65
+ globalThis.fetch = this.originalFetch;
66
+ this.originalFetch = undefined;
67
+ uninstallNetworkGuard();
68
+ }
69
+ /** throws if the connector made a request that had no mock, even if it caught the error */
70
+ verify() {
71
+ if (this.unmocked.length) {
72
+ const list = this.unmocked.map((r) => `${r.method} ${r.url}`).join('\n ');
73
+ throw new Error(`UNMOCKED vendor requests were made (no network is allowed in tests):\n ${list}`);
74
+ }
75
+ }
76
+ /** throws if a registered route was never used */
77
+ verifyAllRoutesUsed() {
78
+ const unused = this.routes.filter((r) => r.used === 0);
79
+ if (unused.length) {
80
+ throw new Error(`mocked routes never called: ${unused.map((r) => `${r.method} ${String(r.matcher)}`).join(', ')}`);
81
+ }
82
+ }
83
+ async handle(input, init = {}) {
84
+ const rawUrl = typeof input === 'string' ? input : input instanceof URL ? input.href : input.url;
85
+ const url = canonical(rawUrl);
86
+ const method = String(init.method || (typeof input === 'object' && input.method) || 'GET').toUpperCase();
87
+ const headers = {};
88
+ new Headers(init.headers || (typeof input === 'object' && input.headers) || {}).forEach((value, key) => {
89
+ headers[key] = value;
90
+ });
91
+ const request = { method, url, rawUrl, headers };
92
+ if (init.body != null)
93
+ request.body = typeof init.body === 'string' ? init.body : String(init.body);
94
+ const route = this.routes.find((r) => r.method === method && r.used < r.times && matches(r.matcher, url));
95
+ if (route) {
96
+ route.used++;
97
+ this.requests.push(request);
98
+ const spec = typeof route.respond === 'function' ? await route.respond(request) : route.respond;
99
+ const responseHeaders = { ...spec.headers };
100
+ let body = null;
101
+ if (spec.json !== undefined) {
102
+ body = JSON.stringify(spec.json);
103
+ responseHeaders['content-type'] ||= 'application/json';
104
+ }
105
+ else if (spec.text !== undefined) {
106
+ body = spec.text;
107
+ }
108
+ const status = spec.status ?? 200;
109
+ return new Response([204, 205, 304].includes(status) ? null : body, { status, headers: responseHeaders });
110
+ }
111
+ if (this.originalFetch && this.passthrough(new URL(url))) {
112
+ return this.originalFetch(input, init);
113
+ }
114
+ this.unmocked.push(request);
115
+ // status 400 makes the SDK Fetcher give up at once instead of retrying for seconds
116
+ const error = new Error(`UNMOCKED vendor request: ${method} ${url} (add a route with vendor.on(...))`);
117
+ error.status = 400;
118
+ error.alomaHarnessUnmocked = true;
119
+ throw error;
120
+ }
121
+ }
@@ -1,5 +1,5 @@
1
- declare const _default: (path: string) => Promise<{
2
- text: any;
1
+ declare const _default: (file: string) => Promise<{
2
+ text: string;
3
3
  methods: string[];
4
4
  }>;
5
5
  export default _default;