@egen-civitas/esm-embed-testing 0.2.0 → 0.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.
package/src/test-host.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { effectiveGrants, type ApprovedGrants, type CapabilityId, type GrantConfig } from '@egen-civitas/esm-embed-protocol';
2
2
  import type { ConnectOptions } from '@egen-civitas/esm-embed-client';
3
- import { createBus, HostConnection, type ApiRequest, type ApiResponse, type HostProviders, type ThemeState } from '@egen-civitas/esm-embed-host';
3
+ import { createBus, HostConnection, type ExtensionInfo, type HostProviders } from '@egen-civitas/esm-embed-host';
4
4
 
5
5
  export interface TestHostOptions {
6
6
  moduleName?: string;
@@ -10,15 +10,12 @@ export interface TestHostOptions {
10
10
  manifestCapabilities?: Partial<Record<CapabilityId, GrantConfig>>;
11
11
  /** Plafond fixé par l'opérateur (défaut : rien d'approuvé hors capacités « par défaut »). */
12
12
  approved?: ApprovedGrants;
13
- session?: Record<string, unknown>;
14
13
  tenant?: { id: string | null };
15
14
  locale?: string;
16
- theme?: ThemeState;
17
15
  config?: Record<string, unknown>;
18
16
  flags?: Record<string, boolean>;
19
- /** Permissions que l'utilisateur simulé possède. */
20
- permissions?: string[];
21
- api?: (req: ApiRequest) => ApiResponse | Promise<ApiResponse>;
17
+ /** Extensions attachées à chaque slot (`{ 'topbar-nav-slot': [...] }`). */
18
+ extensions?: Record<string, ExtensionInfo[]>;
22
19
  confirm?: boolean | ((d: { title: string; message: string; danger?: boolean }) => boolean);
23
20
  }
24
21
 
@@ -34,22 +31,18 @@ export interface TestHost {
34
31
  connection: HostConnection;
35
32
  /** Journal des services du Core sollicités par l'app. */
36
33
  calls: CallRecord[];
37
- notifications: Array<{ kind: string; title: string; subtitle?: string }>;
34
+ notifications: Array<{ id?: string; kind: string; title: string; subtitle?: string }>;
38
35
  published: Array<{ topic: string; payload: unknown; from: string }>;
39
- tools: Map<string, { description: string }>;
40
36
  /** Pousse un changement vers l'app (comme le ferait le Core). */
41
37
  emit: {
42
- session(v: Record<string, unknown>): void;
43
38
  tenant(v: { id: string | null }): void;
44
39
  locale(v: string): void;
45
- theme(v: ThemeState): void;
40
+ extensions(slot: string, list: ExtensionInfo[]): void;
46
41
  config(v: Record<string, unknown>): void;
47
42
  event(topic: string, payload?: unknown): void;
48
43
  goto(path: string): void;
49
44
  visibility(visible: boolean): void;
50
45
  };
51
- /** L'agent IA appelle un outil déclaré par l'app. */
52
- invokeTool(name: string, args?: Record<string, unknown>): Promise<unknown>;
53
46
  destroy(): void;
54
47
  }
55
48
 
@@ -71,8 +64,8 @@ function watchable<T>(initial: T) {
71
64
  * relié à votre app par de fausses fenêtres et un vrai `MessageChannel`. Aucun navigateur, aucun Core.
72
65
  *
73
66
  * ```ts
74
- * const sim = createTestHost({ session: { authenticated: true, user: { display: 'Sam' } } });
75
- * const egen = await connectToEgen({ ...sim.connectOptions, capabilities: ['session'] });
67
+ * const sim = createTestHost({ locale: 'fr', tenant: { id: 'acme' } });
68
+ * const egen = await connectToEgen({ ...sim.connectOptions, capabilities: ['locale', 'tenant'] });
76
69
  * ```
77
70
  */
78
71
  export function createTestHost(o: TestHostOptions = {}): TestHost {
@@ -81,14 +74,12 @@ export function createTestHost(o: TestHostOptions = {}): TestHost {
81
74
  const calls: CallRecord[] = [];
82
75
  const notifications: TestHost['notifications'] = [];
83
76
  const published: TestHost['published'] = [];
84
- const tools = new Map<string, { description: string }>();
85
- const toolInvokers = new Map<string, (name: string, args: Record<string, unknown>) => Promise<unknown>>();
86
77
  const log = (cap: string, method: string, ...args: unknown[]) => void calls.push({ cap, method, args });
87
78
 
88
- const session = watchable<Record<string, unknown> | undefined>(o.session ?? { authenticated: true });
89
79
  const tenant = watchable(o.tenant ?? { id: null });
90
80
  const locale = watchable(o.locale ?? 'fr');
91
- const theme = watchable<ThemeState>(o.theme ?? { mode: 'light', tokens: {} });
81
+ const extList = new Map<string, ExtensionInfo[]>(Object.entries(o.extensions ?? {}));
82
+ const extSubs = new Map<string, Set<(l: ExtensionInfo[]) => void>>();
92
83
  const config = watchable(o.config ?? {});
93
84
  const flagSubs = new Set<() => void>();
94
85
  const eventSubs = new Map<string, Set<(p: unknown) => void>>();
@@ -103,10 +94,8 @@ export function createTestHost(o: TestHostOptions = {}): TestHost {
103
94
  });
104
95
 
105
96
  const providers: HostProviders = {
106
- session: wrapWatch('session', session),
107
97
  tenant: wrapWatch('tenant', tenant),
108
98
  locale: wrapWatch('locale', locale),
109
- theme: wrapWatch('theme', theme),
110
99
  config: wrapWatch('config', config),
111
100
  flags: {
112
101
  isEnabled: (n) => {
@@ -115,10 +104,16 @@ export function createTestHost(o: TestHostOptions = {}): TestHost {
115
104
  },
116
105
  subscribe: (cb) => (flagSubs.add(cb), () => void flagSubs.delete(cb)),
117
106
  },
118
- permissions: {
119
- can: (p) => {
120
- log('permissions', 'can', p);
121
- return !!o.permissions?.includes(p);
107
+ extensions: {
108
+ list: (slot) => {
109
+ log('extensions', 'list', slot);
110
+ return extList.get(slot) ?? [];
111
+ },
112
+ subscribe: (slot, cb) => {
113
+ let set = extSubs.get(slot);
114
+ if (!set) extSubs.set(slot, (set = new Set()));
115
+ set.add(cb);
116
+ return () => void set!.delete(cb);
122
117
  },
123
118
  },
124
119
  events: {
@@ -136,7 +131,10 @@ export function createTestHost(o: TestHostOptions = {}): TestHost {
136
131
  notifications: {
137
132
  show: (n) => {
138
133
  log('notifications', 'show', n);
134
+ // même dédoublonnage que le Core : un `id` déjà affiché n'est pas réaffiché
135
+ if (n.id && notifications.some((x) => x.id === n.id)) return false;
139
136
  notifications.push(n);
137
+ return true;
140
138
  },
141
139
  },
142
140
  dialogs: {
@@ -145,28 +143,7 @@ export function createTestHost(o: TestHostOptions = {}): TestHost {
145
143
  return typeof o.confirm === 'function' ? o.confirm(d) : (o.confirm ?? true);
146
144
  },
147
145
  },
148
- api: {
149
- fetch: async (req) => {
150
- log('api', 'fetch', req);
151
- return o.api ? o.api(req) : { status: 200, headers: { 'content-type': 'application/json' }, body: '{}' };
152
- },
153
- },
154
146
  telemetry: { log: (l, m, c) => log('telemetry', 'log', l, m, c), metric: (n, v, t) => log('telemetry', 'metric', n, v, t) },
155
- aiTools: {
156
- register: (_m, list, invoke) => {
157
- log('ai-tools', 'register', list.map((t) => t.name));
158
- for (const t of list) {
159
- tools.set(t.name, { description: t.description });
160
- toolInvokers.set(t.name, invoke);
161
- }
162
- },
163
- unregister: (_m, names) => {
164
- for (const n of names) {
165
- tools.delete(n);
166
- toolInvokers.delete(n);
167
- }
168
- },
169
- },
170
147
  };
171
148
 
172
149
  const appWindow = new EventTarget();
@@ -202,22 +179,18 @@ export function createTestHost(o: TestHostOptions = {}): TestHost {
202
179
  calls,
203
180
  notifications,
204
181
  published,
205
- tools,
206
182
  emit: {
207
- session: (v) => session.set(v),
208
183
  tenant: (v) => tenant.set(v),
209
184
  locale: (v) => locale.set(v),
210
- theme: (v) => theme.set(v),
185
+ extensions: (slot, list) => {
186
+ extList.set(slot, list);
187
+ extSubs.get(slot)?.forEach((cb) => cb(list));
188
+ },
211
189
  config: (v) => config.set(v),
212
190
  event: (topic, payload) => eventSubs.get(topic)?.forEach((cb) => cb(payload)),
213
191
  goto: (path) => channel.emit('goto', { path, seq: Date.now() }),
214
192
  visibility: (v) => channel.emit('visibility', v),
215
193
  },
216
- invokeTool: async (name, args = {}) => {
217
- const inv = toolInvokers.get(name);
218
- if (!inv) throw new Error(`Outil non enregistré : ${name}`);
219
- return inv(name, args);
220
- },
221
194
  destroy: () => connection.destroy(),
222
195
  };
223
196
  }