@vivoa/plugin-sdk 0.6.3 → 0.6.6

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.
@@ -0,0 +1,113 @@
1
+ import {
2
+ checkPermissions
3
+ } from "../chunk-DGJZW46B.mjs";
4
+
5
+ // src/testing/mock-host.ts
6
+ var DEFAULT_RESPONSE = { status: 200, headers: {}, body: {} };
7
+ function createMockHostApi(opts = {}) {
8
+ const calls = {
9
+ "http.request": [],
10
+ "secrets.get": [],
11
+ "storage.getUrl": [],
12
+ "storage.put": [],
13
+ "sequence.next": [],
14
+ "host.on": []
15
+ };
16
+ const registered = [];
17
+ const handlers = /* @__PURE__ */ new Map();
18
+ const seqState = new Map(
19
+ Object.entries(opts.sequences ?? {})
20
+ );
21
+ const enforce = (required) => {
22
+ if (!opts.grantedPermissions) return;
23
+ const check = checkPermissions([required], opts.grantedPermissions);
24
+ if (!check.ok) {
25
+ throw new Error(
26
+ `MockHostApi: permission denied \u2014 plugin needs "${required}" but it is not in grantedPermissions. Declare it in your manifest.`
27
+ );
28
+ }
29
+ };
30
+ const noop = () => {
31
+ };
32
+ const logger = {
33
+ debug: noop,
34
+ info: noop,
35
+ warn: noop,
36
+ error: noop
37
+ };
38
+ const http = {
39
+ // async so a denied-permission throw surfaces as a rejected promise, not a
40
+ // synchronous throw — matches the real host's async port contract.
41
+ request: async (req) => {
42
+ const hostname = safeHostname(req.url);
43
+ enforce(`http.outbound:${hostname}`);
44
+ calls["http.request"].push(req);
45
+ return opts.httpResponses?.[req.url] ?? Object.entries(opts.httpResponses ?? {}).find(
46
+ ([k]) => req.url.includes(k)
47
+ )?.[1] ?? DEFAULT_RESPONSE;
48
+ }
49
+ };
50
+ const secrets = {
51
+ get: async (key) => {
52
+ enforce(`secrets.read:${key}`);
53
+ calls["secrets.get"].push(key);
54
+ return opts.secrets?.[key];
55
+ }
56
+ };
57
+ const storage = {
58
+ getUrl: (key) => {
59
+ calls["storage.getUrl"].push(key);
60
+ return Promise.resolve(`mock://storage/${key}`);
61
+ },
62
+ put: (key, body) => {
63
+ calls["storage.put"].push({ key, body });
64
+ return Promise.resolve();
65
+ }
66
+ };
67
+ const sequence = {
68
+ next: (scope) => {
69
+ calls["sequence.next"].push(scope);
70
+ const current = seqState.get(scope) ?? 0;
71
+ const next = current + 1;
72
+ seqState.set(scope, next);
73
+ return Promise.resolve(next);
74
+ }
75
+ };
76
+ const host = {
77
+ logger,
78
+ http,
79
+ secrets,
80
+ storage,
81
+ sequence,
82
+ calls,
83
+ registered,
84
+ handlers,
85
+ on: (event, handler) => {
86
+ calls["host.on"].push(event);
87
+ const list = handlers.get(event) ?? [];
88
+ list.push(handler);
89
+ handlers.set(event, list);
90
+ },
91
+ registerCapability: (cap) => {
92
+ registered.push(cap);
93
+ },
94
+ emit: async (event, payload) => {
95
+ for (const h of handlers.get(event) ?? []) await h(payload);
96
+ }
97
+ };
98
+ return host;
99
+ }
100
+ function safeHostname(url) {
101
+ try {
102
+ return new URL(url).hostname;
103
+ } catch {
104
+ return url;
105
+ }
106
+ }
107
+ function createDispatchContext(config = {}, overrides = {}) {
108
+ return { config, ...overrides };
109
+ }
110
+ export {
111
+ createDispatchContext,
112
+ createMockHostApi
113
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vivoa/plugin-sdk",
3
- "version": "0.6.3",
3
+ "version": "0.6.6",
4
4
  "description": "Public contracts for Vivoa plugins (types, DI tokens, config + setup-layout validators)",
5
5
  "license": "MIT",
6
6
  "author": "Vivoa",
@@ -24,6 +24,11 @@
24
24
  "types": "./dist/index.d.ts",
25
25
  "import": "./dist/index.mjs",
26
26
  "require": "./dist/index.js"
27
+ },
28
+ "./testing": {
29
+ "types": "./dist/testing/index.d.ts",
30
+ "import": "./dist/testing/index.mjs",
31
+ "require": "./dist/testing/index.js"
27
32
  }
28
33
  },
29
34
  "sideEffects": false,
@@ -44,8 +49,8 @@
44
49
  "vitest": "^2.1.8"
45
50
  },
46
51
  "scripts": {
47
- "build": "tsup src/index.ts --format cjs,esm --dts --clean --target node18",
48
- "dev": "tsup src/index.ts --format cjs,esm --dts --watch --target node18",
52
+ "build": "tsup src/index.ts src/testing/index.ts --format cjs,esm --dts --clean --target node18",
53
+ "dev": "tsup src/index.ts src/testing/index.ts --format cjs,esm --dts --watch --target node18",
49
54
  "test": "vitest run",
50
55
  "test:watch": "vitest",
51
56
  "typecheck": "tsc --noEmit -p tsconfig.json"