@coda-console/plugin-check 0.8.21 → 0.8.23
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/check/coda/adapters/plugin-events.mjs +38 -0
- package/check/coda/adapters/plugin-modules.mjs +20 -9
- package/check/coda/adapters/plugin-test-host.d.mts +39 -0
- package/check/coda/adapters/plugin-test-host.mjs +40 -9
- package/check/coda/application/host-protocols.mjs +17 -0
- package/check/coda/builtin.json +977 -62
- package/check/coda/components/plugins/plugin-extensions.mjs +91 -25
- package/check/coda/components/plugins/plugin-rules.mjs +3 -3
- package/check/coda/components/plugins/plugin-source-rules.mjs +21 -11
- package/check/coda/components/protocols/protocol-events.mjs +49 -0
- package/check/coda/components/protocols/record-rules.mjs +13 -8
- package/check/coda/components/protocols/types.mjs +4 -4
- package/check/coda/components/workflows/contributions.mjs +2 -2
- package/check/coda/components/workflows/workflow-protocol-definitions.mjs +362 -0
- package/check/coda/contract.json +19 -12
- package/check/coda/kernel/plugin-protocol.mjs +56 -0
- package/check/coda/kernel/project-protocol.mjs +52 -0
- package/check/coda/kernel/thread-protocol.mjs +167 -0
- package/check/manifest.mjs +24 -2
- package/package.json +2 -2
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// A module's events (docs/plugins.md「事件」), as its `coda` has them (adapters/plugin-modules.mjs): `emit` publishes one of a
|
|
2
|
+
// protocol its plugin defines or implements, through the backend, which knows the plugin by the module's token (`asks`);
|
|
3
|
+
// `on` handles one its plugin subscribes to (`plugin.events`, the events of the event groups it uses, as the backend reckons
|
|
4
|
+
// them: components/plugins/plugin-events.mjs `subscribed`), as the backend delivers it (`deliver`). A handler that throws
|
|
5
|
+
// is said in the log, and is not handed the event again.
|
|
6
|
+
import { codaError } from './provider-api.mjs';
|
|
7
|
+
|
|
8
|
+
export function pluginEvents(plugin, asks, fire) {
|
|
9
|
+
const subscribed = new Set(plugin.events ?? []), handlers = new Map();
|
|
10
|
+
return {
|
|
11
|
+
api: {
|
|
12
|
+
// `emit(name, data, { subject, project })`: published once the backend has checked it, what is handed on after not
|
|
13
|
+
// waited for; refused with its code, nothing published.
|
|
14
|
+
async emit(name, data, { subject, project } = {}) {
|
|
15
|
+
if (typeof name !== 'string' || typeof subject !== 'string' || typeof project !== 'string') throw new TypeError('emit(事件名, data, { subject, project })');
|
|
16
|
+
await asks('emit', { name, data, subject, project });
|
|
17
|
+
},
|
|
18
|
+
// `on(name, fn)`: `fn` given each event of the name delivered (`{ name, kind, subject, project, data, publisher }`)
|
|
19
|
+
// until it is disposed, or the module stops.
|
|
20
|
+
on(name, fn) {
|
|
21
|
+
if (typeof name !== 'string' || typeof fn !== 'function') throw new TypeError('on(事件名, fn)');
|
|
22
|
+
if (!subscribed.has(name)) throw codaError('forbidden', `插件 ${plugin.id} 没有订事件 ${name}:须在清单的 uses 里写含有它的事件组`, { effects: 'none' });
|
|
23
|
+
// Each its own, however often the same `fn` is.
|
|
24
|
+
const entry = { fn, disposed: false };
|
|
25
|
+
if (!handlers.has(name)) handlers.set(name, new Set());
|
|
26
|
+
handlers.get(name).add(entry);
|
|
27
|
+
return { dispose: () => { entry.disposed = true; handlers.get(name)?.delete(entry); } };
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
// An event the backend delivers: to each handler of its name as it comes, once, but one disposed meanwhile; not to one
|
|
31
|
+
// added meanwhile.
|
|
32
|
+
deliver(event) {
|
|
33
|
+
for (const entry of [...handlers.get(event?.name) ?? []]) if (!entry.disposed) fire([entry.fn], event);
|
|
34
|
+
},
|
|
35
|
+
// The module stopped: its handlers go.
|
|
36
|
+
clear() { handlers.clear(); },
|
|
37
|
+
};
|
|
38
|
+
}
|
|
@@ -8,13 +8,14 @@
|
|
|
8
8
|
// `open { instance, plugin, view, context, caller }`, `query { plugin, request, deadline }`, `run { plugin, call }` and
|
|
9
9
|
// `resume { plugin, call }`, and `cancel { id, reason }` of any of them, `mode { plugin, mode, hook, context }` (a request
|
|
10
10
|
// each), and `message`, `callProgress { id, value }` (a `call`'s progress, by the request's ID), `context`, `close { instance, … }`, `changed { watch, events }`, `settingsChanged { plugin,
|
|
11
|
-
// settings }` (a notice, no answer: the plugin's own settings, as `Plugins.setSettings` wrote them),
|
|
11
|
+
// settings }` (a notice, no answer: the plugin's own settings, as `Plugins.setSettings` wrote them), `event { plugin, event }`
|
|
12
|
+
// (a notice: an event it subscribes to, adapters/plugin-events.mjs), and `output`, `exited`
|
|
12
13
|
// of a process it started (adapters/process-relay.mjs). To it: `post { instance, message }`, `change { instance,
|
|
13
14
|
// collection, type, id, object }`, `unwatch { watch }`, `cancel { id }` of a `query` or a `question`, `progress { run,
|
|
14
15
|
// value }` of a `run`, `drained`, `kill` of a process, and the requests `environment { of, project }`, `settings`
|
|
15
16
|
// (the plugin's own, as they are now), `watch { collection, selector }`, `tell { session, text }`, `part { part, method,
|
|
16
17
|
// args }`, `provider { method, args }`, `serviceRegister { id, port, secret }`, `serviceUnregister { id }`, `query { record,
|
|
17
|
-
// operation, input, timeout, deadline }`, `call { record, operation, input, id, timeout, deadline, under, progress }`, `spawn { under, process, command, args, cwd, env, name }`, each with the
|
|
18
|
+
// operation, input, timeout, deadline }`, `emit { name, data, subject, project }`, `call { record, operation, input, id, timeout, deadline, under, progress }`, `spawn { under, process, command, args, cwd, env, name }`, each with the
|
|
18
19
|
// module's `token`, by which the backend knows the plugin it is of, never by what it names; and `question { run,
|
|
19
20
|
// question }` and `input` of a process. A query's
|
|
20
21
|
// failure crosses as `failureOf` says it, as one that did nothing (`none`); a run's end, and a resume's, as its outcome, or a resume's as null when the
|
|
@@ -25,6 +26,7 @@ import { pluginServices } from './plugin-stream-services.mjs';
|
|
|
25
26
|
import { pathToFileURL } from 'node:url';
|
|
26
27
|
import { callingAPI, canceled, codaError, failed, failureOf, givenUp, providerAPI, queryingAPI } from './provider-api.mjs';
|
|
27
28
|
import { queryId } from './plugin-command.mjs';
|
|
29
|
+
import { pluginEvents } from './plugin-events.mjs';
|
|
28
30
|
|
|
29
31
|
export function pluginModules({ send }) {
|
|
30
32
|
// This machine's Coda, as `start` gives it: its address only. Each module reaches it with its own plugin's token, given
|
|
@@ -95,7 +97,7 @@ export function pluginModules({ send }) {
|
|
|
95
97
|
// One of Coda's collections, whole or as `selector` has it (a session's runs), watched once for each plugin (`plugin`)
|
|
96
98
|
// however many of its modules' watchers watch it, asked with its token (`asks`): `fn` is given every object as `ADDED`
|
|
97
99
|
// first, then each change. Done once the first are given. The last watcher gone, the backend stops reading it. A kind's
|
|
98
|
-
// records (coda#2749 3.7, `{ scope?, id? }` as `list` takes them) are each told as an entry, as `get` and `list` give it
|
|
100
|
+
// records (coda#2749 3.7, `{ scope?, id?, labels? }` as `list` takes them) are each told as an entry, as `get` and `list` give it
|
|
99
101
|
// to the plugin (its own with their keys), by its ID: `{ type, id, entry }`.
|
|
100
102
|
// A change as a watcher is told it: of a kind, its entry by the record's ID; the backend tells it by its holder's too.
|
|
101
103
|
const told = (watched, type, id, object) => watched.entries ? { type, id: object.id, entry: object } : { type, id, object };
|
|
@@ -205,6 +207,8 @@ export function pluginModules({ send }) {
|
|
|
205
207
|
const environment = (project = null, options = {}) => asks('environment', { of: options?.plugin ?? null, project });
|
|
206
208
|
// The listeners of `settings.onChange`: this plugin's own settings only, told as they are written.
|
|
207
209
|
const settingsChanges = new Set();
|
|
210
|
+
// The events it publishes and those it subscribes to (adapters/plugin-events.mjs).
|
|
211
|
+
const events = pluginEvents(plugin, asks, fire);
|
|
208
212
|
// A query through the backend's unified call, the caller this plugin by its token.
|
|
209
213
|
const querying = queryingAPI({ send: (request, signal) => asks('query', request, signal), plugin, operations });
|
|
210
214
|
// A doing call through the call kernel, from this plugin's address (docs/plugins.md「做事的调用」).
|
|
@@ -232,7 +236,7 @@ export function pluginModules({ send }) {
|
|
|
232
236
|
// `put({ kind, key }, { spec, status?, labels? })` made or replaced (a status not given kept), answered with its entry,
|
|
233
237
|
// `delete({ kind, key })`, and `replace(kind, entries)`, every one of a kind the plugin holds given (`[{ key, spec,
|
|
234
238
|
// status?, labels? }]`), those it no longer gives gone, answered with the entries given, in their order: each named by
|
|
235
|
-
// the plugin's own key, its ID the host's. `get({ kind, id })`, one record or null, and `list(kind, { scope?, id? })`,
|
|
239
|
+
// the plugin's own key, its ID the host's. `get({ kind, id })`, one record or null, and `list(kind, { scope?, id?, labels? })`,
|
|
236
240
|
// the records of any kind, each an entry `{ owner, location, id, plugin, key?, local, spec, status, labels, version }`, `key`
|
|
237
241
|
// only of this plugin's own here: read after what this module wrote before.
|
|
238
242
|
get: record => writing.then(() => asks('provider', { method: 'getRecord', args: [record] })),
|
|
@@ -282,11 +286,13 @@ export function pluginModules({ send }) {
|
|
|
282
286
|
get: () => asks('settings', {}),
|
|
283
287
|
onChange: listen(settingsChanges),
|
|
284
288
|
}),
|
|
285
|
-
// Coda's own collections, as a page watches the module's (`
|
|
286
|
-
//
|
|
289
|
+
// Coda's own collections, as a page watches the module's (`threads`: the sessions this machine lists), or a kind's
|
|
290
|
+
// records by its protocol's name.
|
|
287
291
|
watch: watching,
|
|
292
|
+
// Events (docs/plugins.md「事件」): `emit` one of its plugin's protocols, `on` one it subscribes to.
|
|
293
|
+
...events.api,
|
|
288
294
|
});
|
|
289
|
-
return { coda, views, protocols, operations, services, querying, calling, spawnFor, settingsChanges };
|
|
295
|
+
return { coda, views, protocols, operations, services, querying, calling, spawnFor, settingsChanges, events };
|
|
290
296
|
}
|
|
291
297
|
|
|
292
298
|
// Loaded once, as the backend activates it (`activate`, the first time anything of it is needed); what else comes for it
|
|
@@ -449,6 +455,11 @@ export function pluginModules({ send }) {
|
|
|
449
455
|
const made = await loading.get(plugin)?.catch(() => null);
|
|
450
456
|
if (made) fire(made.settingsChanges, settings);
|
|
451
457
|
},
|
|
458
|
+
// An event the plugin subscribes to (`coda.on`), as the backend delivers it to a module activated already.
|
|
459
|
+
async event({ plugin, event }) {
|
|
460
|
+
const made = await loading.get(plugin)?.catch(() => null);
|
|
461
|
+
made?.events.deliver(event);
|
|
462
|
+
},
|
|
452
463
|
close({ instance }) {
|
|
453
464
|
const shown = instances.get(instance);
|
|
454
465
|
if (shown) { instances.delete(instance); fire(shown.listeners.close); }
|
|
@@ -491,8 +502,8 @@ export function pluginModules({ send }) {
|
|
|
491
502
|
let stopping = null;
|
|
492
503
|
const stop = () => stopping ??= Promise.allSettled([...processes.values()].map(entry => entry.interrupt())
|
|
493
504
|
.concat([...loading.values()].map(async loaded => {
|
|
494
|
-
const { module, services, settingsChanges } = await loaded;
|
|
495
|
-
settingsChanges.clear();
|
|
505
|
+
const { module, services, settingsChanges, events } = await loaded;
|
|
506
|
+
settingsChanges.clear(); events.clear();
|
|
496
507
|
await services.close(false);
|
|
497
508
|
if (typeof module?.deactivate === 'function') await module.deactivate();
|
|
498
509
|
})));
|
|
@@ -179,6 +179,22 @@ export interface RecordFilter { scope?: 'local' | 'available'; id?: string }
|
|
|
179
179
|
/** A record read anew, made or gone: by its ID, as it is now, or as it was last. */
|
|
180
180
|
export interface RecordChange { type: 'ADDED' | 'MODIFIED' | 'DELETED'; id: string; entry: Entry }
|
|
181
181
|
|
|
182
|
+
/** An event, as a module's handler of it is given it (`coda.on`). */
|
|
183
|
+
export interface Event {
|
|
184
|
+
/** Its name, the full dotted one (`pull_request.closed`), one on this machine. */
|
|
185
|
+
name: string;
|
|
186
|
+
/** The protocol it is of, with its major version (`github/pull-request@1`): the event's version. */
|
|
187
|
+
kind: string;
|
|
188
|
+
/** What it is about, `<kind>:<id>`. */
|
|
189
|
+
subject: string;
|
|
190
|
+
/** The ID of the project of this machine it was published in. */
|
|
191
|
+
project: string;
|
|
192
|
+
/** What it says, as its protocol's payload schema has it. */
|
|
193
|
+
data: { [key: string]: Json };
|
|
194
|
+
/** The ID of the plugin that published it; `coda` for Coda's own. */
|
|
195
|
+
publisher: string;
|
|
196
|
+
}
|
|
197
|
+
|
|
182
198
|
export interface Coda {
|
|
183
199
|
/** Handles an operation of a protocol the plugin implements: a query (`safe`) or a doing call (any other, `idempotent` among them). */
|
|
184
200
|
handle(kind: string, operation: string, handler: Handler | { run: Handler; resume?: Resume }): Disposable;
|
|
@@ -235,6 +251,18 @@ export interface Coda {
|
|
|
235
251
|
* the entries given, in their order.
|
|
236
252
|
*/
|
|
237
253
|
replace(kind: string, entries: (RecordValue & { key: string })[]): Promise<Entry[]>;
|
|
254
|
+
/**
|
|
255
|
+
* Publishes an event of a protocol the plugin defines or implements, not one of Coda's own (those only Coda publishes),
|
|
256
|
+
* about `subject` in `project` (a project of this machine, by ID, the plugin applying there): done once Coda has checked
|
|
257
|
+
* it, its delivery not waited for; refused (`invalid_input`), nothing was published.
|
|
258
|
+
*/
|
|
259
|
+
emit(name: string, data: { [key: string]: Json }, options: { subject: string; project: string }): Promise<void>;
|
|
260
|
+
/**
|
|
261
|
+
* Handles an event the plugin subscribes to: one of an event group in its `uses`, any other throwing at once (`forbidden`).
|
|
262
|
+
* Given each one published in a project the plugin applies in, at most once, while the module is activated; not sorted by
|
|
263
|
+
* subject. What it throws is logged; the event is not given again.
|
|
264
|
+
*/
|
|
265
|
+
on(name: string, fn: (event: Event) => void): Disposable;
|
|
238
266
|
}
|
|
239
267
|
|
|
240
268
|
/** A protocol a plugin implements, some of its operations by commands: operation name to the file that implements it. */
|
|
@@ -261,6 +289,8 @@ export interface TestPlugin {
|
|
|
261
289
|
protocolImplementations?: (string | ProtocolImplementation)[];
|
|
262
290
|
/** The names of the processes it declares (`contributes.processes`): a named `spawn` of any other fails (`not_found`). */
|
|
263
291
|
processes?: string[];
|
|
292
|
+
/** The permission groups it uses (`uses`), by protocol: its event groups are the events its `coda.on` may name. */
|
|
293
|
+
uses?: Record<string, string[]>;
|
|
264
294
|
}
|
|
265
295
|
|
|
266
296
|
export interface TestCallOptions extends QueryOptions {
|
|
@@ -293,6 +323,13 @@ export interface TestHost {
|
|
|
293
323
|
resume(record: Ref, operation: string, call?: { id?: string; input?: Json; progress?: Json | null; canceled?: boolean; deadline?: number | null }): Promise<Json>;
|
|
294
324
|
/** Writes the plugin's settings again, as the backend does: `coda.settings.get` gives them, and `onChange` is told. */
|
|
295
325
|
setSettings(settings: Json): Promise<void>;
|
|
326
|
+
/** Delivers an event to the module as Coda does: to its handlers of the name (`coda.on`) now. */
|
|
327
|
+
emit(event: Event): Promise<void>;
|
|
328
|
+
/**
|
|
329
|
+
* The events the module published (`coda.emit`), in order, each checked as Coda checks it but for its project: of a
|
|
330
|
+
* protocol the plugin defines or implements, not one of Coda's own, as its payload schema has it.
|
|
331
|
+
*/
|
|
332
|
+
emitted: Omit<Event, 'publisher'>[];
|
|
296
333
|
close(): Promise<unknown>;
|
|
297
334
|
}
|
|
298
335
|
|
|
@@ -311,6 +348,8 @@ export function createTestHost(options?: {
|
|
|
311
348
|
* this machine (`TEST_OWNER`). The space's 64 KiB a record is not held to here.
|
|
312
349
|
*/
|
|
313
350
|
records?: (Ref & RecordValue & { plugin?: string; owner?: string; location?: string })[];
|
|
351
|
+
/** Others' protocol definitions the plugin uses event groups of. */
|
|
352
|
+
protocols?: object[];
|
|
314
353
|
}): TestHost;
|
|
315
354
|
/** This machine's backend in the test host: the `owner` and `location` of the records the module writes. */
|
|
316
355
|
export const TEST_OWNER: string;
|
|
@@ -14,7 +14,9 @@
|
|
|
14
14
|
//
|
|
15
15
|
// What the module queries (`coda.query`, `ctx.query`) of a record of a protocol it implements its own handlers answer; of
|
|
16
16
|
// another's, `call` given. A doing call it makes (`coda.call`, `ctx.call`) its own handlers answer, of a protocol it implements. Its settings (`coda.settings`) are those the test gives, or sets again with `setSettings`, which
|
|
17
|
-
// tells the module's `onChange` as the backend does.
|
|
17
|
+
// tells the module's `onChange` as the backend does. The events it publishes (`coda.emit`) are kept for the test to read
|
|
18
|
+
// (`emitted`); those the test delivers (`emit`) go to its handlers (`coda.on`), which may name only what it subscribes to by
|
|
19
|
+
// its `uses`, as the backend has it. What it starts (`coda.spawn`, `ctx.spawn`) is a real child process of
|
|
18
20
|
// the test's, its streams and its end relayed as the backend relays them (adapters/process-relay.mjs), with no token and
|
|
19
21
|
// no entry in a registry: ended with the call or the module only as the test ends it, or as the host closes. The records
|
|
20
22
|
// (`coda.get`, `list`, `watch`, `put`, `delete`, `replace`, coda#2749 3.7) are those it writes, on this one machine, by its
|
|
@@ -22,15 +24,18 @@
|
|
|
22
24
|
// to what the backend holds them to (components/protocols/record-rules.mjs, the kinds' schemas) but the record directory's
|
|
23
25
|
// size. Anything else of the backend's it asks is refused. A command implementing an operation is started as the module's
|
|
24
26
|
// processes are, with no token: what it asks with `coda` (`progress`, `ask`, `query`, `self`) is not answered here, but by the backend's.
|
|
25
|
-
import { CORE_PROTOCOLS } from '@coda-console/protocol/protocol';
|
|
27
|
+
import { CORE_PROTOCOLS, protocolHasRecords } from '@coda-console/protocol/protocol';
|
|
26
28
|
import { spawn } from 'node:child_process';
|
|
27
29
|
import { randomUUID } from 'node:crypto';
|
|
28
30
|
import { ContractError } from '../domain/errors.mjs';
|
|
29
31
|
import { ProtocolTypes } from '../components/protocols/types.mjs';
|
|
30
32
|
import { commandOf, implementations } from '../components/plugins/plugin-rules.mjs';
|
|
33
|
+
import { publishedProblem, subscribed } from '../components/plugins/plugin-extensions.mjs';
|
|
34
|
+
import { protocolEvents } from '../components/protocols/protocol-events.mjs';
|
|
35
|
+
import { HOST_PROTOCOLS } from '../application/host-protocols.mjs';
|
|
31
36
|
import { checkGiven, checkHeld, checkKept, groupType, keptStatus } from '../components/concurrency-groups/schemas.mjs';
|
|
32
37
|
import { validate } from '../components/protocols/types.mjs';
|
|
33
|
-
import { LOCAL_KINDS, checkLabels, checkReplace, checkWrite, picked, readFilter, readRef, recordId, writeRef } from '../components/protocols/record-rules.mjs';
|
|
38
|
+
import { LOCAL_KINDS, checkLabels, checkReplace, checkWrite, labelled, picked, readFilter, readRef, recordId, writeRef } from '../components/protocols/record-rules.mjs';
|
|
34
39
|
import { canonical } from '../domain/values.mjs';
|
|
35
40
|
import { checkInput, checkRequest, checkResult, limit, stopped } from './call-limit.mjs';
|
|
36
41
|
import { Clock } from './clock.mjs';
|
|
@@ -53,18 +58,27 @@ const open = { type: 'object' };
|
|
|
53
58
|
// the calls' deadlines go by (adapters/clock.mjs), for a test to move. `settings`: the plugin's settings to begin with.
|
|
54
59
|
// `records`: others' records the module reads, each `{ kind, id, spec, status?, labels?, plugin?, owner?, location? }`,
|
|
55
60
|
// another machine's (`owner`, by default `other`; `plugin`, by default the plugin under test) or, `owner` this one's
|
|
56
|
-
// (`TEST_OWNER`), another plugin's here (`plugin`, by default `other`).
|
|
57
|
-
|
|
61
|
+
// (`TEST_OWNER`), another plugin's here (`plugin`, by default `other`). `protocols`: others' protocol definitions, of the
|
|
62
|
+
// event groups the plugin uses (its `uses`), as the backend reckons what its `coda.on` may name
|
|
63
|
+
// (components/plugins/plugin-events.mjs `subscribed`).
|
|
64
|
+
export function createTestHost({ plugin = {}, call = null, clock = new Clock(), settings = {}, records = [], protocols = [] } = {}) {
|
|
58
65
|
// The protocols as the backend compiles them: Coda's own, those of its concurrency groups (RunGroups.attach), those it
|
|
59
66
|
// keeps out of the record directory, and the plugin's.
|
|
60
67
|
const types = new ProtocolTypes();
|
|
61
68
|
for (const { name, spec, status, operations = {} } of [...Object.values(CORE_PROTOCOLS), { ...groupType, name: groupType.kind },
|
|
62
|
-
...(plugin.protocols ?? [])]) types.register({ kind: name, spec, status, operations });
|
|
69
|
+
...(plugin.protocols ?? []).filter(protocolHasRecords)]) types.register({ kind: name, spec, status, operations });
|
|
63
70
|
for (const kind of LOCAL_KINDS) if (!types.types.has(kind)) types.register({ kind, spec: open, status: open, operations: {} });
|
|
64
71
|
const manifest = { protocolImplementations: implementations(plugin.protocolImplementations ?? []) };
|
|
65
72
|
const implemented = manifest.protocolImplementations.map(entry => entry.kind);
|
|
66
73
|
const operationOf = (kind, operation) => types.types.get(kind)?.operations.get(operation);
|
|
67
|
-
|
|
74
|
+
// The plugin, and what the backend would have of it and of the other plugins as its catalog: its protocols, what it
|
|
75
|
+
// implements and uses, and the others' protocols.
|
|
76
|
+
const id = plugin.id ?? 'test', entry = { id, protocols: plugin.protocols ?? [], protocolImplementations: manifest.protocolImplementations, uses: plugin.uses ?? {} };
|
|
77
|
+
const catalog = new Map([[id, entry], [' ', { protocols }]]);
|
|
78
|
+
const described = { id, path: plugin.path ?? process.cwd(), state: plugin.state ?? null, protocolImplementations: implemented,
|
|
79
|
+
events: subscribed(entry, catalog, () => null) };
|
|
80
|
+
// The events the module published (`coda.emit`), in order.
|
|
81
|
+
const emitted = [];
|
|
68
82
|
// Requests to the module waiting for its answer, the module's queries under way, and the doing calls handed to it, with what
|
|
69
83
|
// their caller is told of them, by the request's ID each.
|
|
70
84
|
const waiting = new Map(), querying = new Map(), runs = new Map();
|
|
@@ -94,8 +108,8 @@ export function createTestHost({ plugin = {}, call = null, clock = new Clock(),
|
|
|
94
108
|
kindOf(kind).set(keyOf({ owner, id }), entryOf({ owner, location, id, plugin, spec, status, labels }));
|
|
95
109
|
// As the plugin reads them: its own with their keys.
|
|
96
110
|
const listed = (kind, filter) => {
|
|
97
|
-
const { scope, id } = readFilter(filter), records = kindOf(kind), mine = held.get(kind);
|
|
98
|
-
return [...records.values()].filter(
|
|
111
|
+
const { scope, id, labels } = readFilter(filter), records = kindOf(kind), mine = held.get(kind);
|
|
112
|
+
return [...records.values()].filter(entry => (scope === 'available' || entry.local) && (id === undefined || entry.id === id) && labelled(entry.labels, labels))
|
|
99
113
|
.sort(({ local: one }, { local: other }) => Number(other) - Number(one))
|
|
100
114
|
.map(entry => entry.local && mine.has(entry.id) ? { ...entry, key: mine.get(entry.id) } : entry);
|
|
101
115
|
};
|
|
@@ -185,6 +199,18 @@ export function createTestHost({ plugin = {}, call = null, clock = new Clock(),
|
|
|
185
199
|
});
|
|
186
200
|
}
|
|
187
201
|
if (message.method === 'settings') return reply({ result: JSON.parse(JSON.stringify(current)) });
|
|
202
|
+
// An event the module publishes, checked as the backend checks it (components/plugins/plugin-extensions.mjs
|
|
203
|
+
// `publishedProblem`) but for its project, which is none here: among the events of Coda's own protocols too, which
|
|
204
|
+
// only Coda publishes.
|
|
205
|
+
if (message.method === 'emit') {
|
|
206
|
+
return answered(() => {
|
|
207
|
+
const { token, name, data, ...about } = message.params, event = protocolEvents(catalog, HOST_PROTOCOLS).get(name);
|
|
208
|
+
const problem = publishedProblem('coda.emit', entry, event, { name, subject: about.subject, payload: data });
|
|
209
|
+
if (problem) throw refused('invalid_input', problem);
|
|
210
|
+
emitted.push(structuredClone({ name, kind: event.kind, ...about, data }));
|
|
211
|
+
return null;
|
|
212
|
+
});
|
|
213
|
+
}
|
|
188
214
|
// A doing call's question, answered as its caller's `answer` says; one at a time, as the backend has it
|
|
189
215
|
// (components/plugins/hosted-calls.mjs `question`).
|
|
190
216
|
if (message.method === 'question') {
|
|
@@ -387,6 +413,11 @@ export function createTestHost({ plugin = {}, call = null, clock = new Clock(),
|
|
|
387
413
|
if (said === null) throw new ContractError('interrupted', `插件没有给 ${name} 写 resume`, { effects: 'unknown' });
|
|
388
414
|
return ended(found, name, said);
|
|
389
415
|
},
|
|
416
|
+
// An event delivered to the module as the backend delivers one (components/plugins/plugin-events.mjs), a copy of it: to
|
|
417
|
+
// the handlers of its name (`coda.on`) now, none of it kept. `{ name, kind, subject, project, data, publisher }`.
|
|
418
|
+
emit: event => modules.receive({ method: 'event', params: { plugin: described.id, event: structuredClone(event) } }),
|
|
419
|
+
// The events the module published (`coda.emit`), in order: `{ name, kind, subject, project, data }` each.
|
|
420
|
+
emitted,
|
|
390
421
|
// The plugin's settings written again, as `Plugins.setSettings` leaves them: what the module's `settings.get` gives from now
|
|
391
422
|
// on, and what its `settings.onChange` listeners are told, which have been by the time this is done.
|
|
392
423
|
setSettings: async next => {
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// Coda's own protocols (docs/plugins.md「用到的权限」), each as its component defines it: data that nothing running changes,
|
|
2
|
+
// listed once. The service registers them from here, each bound by its kind to what serves it (components/protocols/protocols.mjs
|
|
3
|
+
// `bind`: application/assembly.mjs; a composer's and a group's own component binds that one), none left unbound once it is
|
|
4
|
+
// put together (server.mjs `checkBound`); and the plugins are judged against them from the first reading of the catalog on, before any is
|
|
5
|
+
// registered (components/plugins/plugins.mjs `protocols`). The plugin host for a plugin's tests takes Coda's events from here
|
|
6
|
+
// too (adapters/plugin-test-host.mjs), so nothing this imports runs. The protocol package's core protocols are not among them
|
|
7
|
+
// (CORE_PROTOCOLS).
|
|
8
|
+
import { PLUGIN_PROTOCOL } from '../kernel/plugin-protocol.mjs';
|
|
9
|
+
import { PROJECT_PROTOCOL } from '../kernel/project-protocol.mjs';
|
|
10
|
+
import { THREAD_PROTOCOL } from '../kernel/thread-protocol.mjs';
|
|
11
|
+
import { composerType, submissionType } from '../components/composers/schemas.mjs';
|
|
12
|
+
import { groupType } from '../components/concurrency-groups/schemas.mjs';
|
|
13
|
+
import { WORKFLOW_PROTOCOL, WORKFLOW_RUN_PROTOCOL, withWorkflows } from '../components/workflows/workflow-protocol-definitions.mjs';
|
|
14
|
+
|
|
15
|
+
// A session's, with the workflows' query and the group that has it (workflow-protocol-definitions.mjs `withWorkflows`).
|
|
16
|
+
export const THREAD_DEFINITION = withWorkflows(THREAD_PROTOCOL);
|
|
17
|
+
export const HOST_PROTOCOLS = [THREAD_DEFINITION, PROJECT_PROTOCOL, PLUGIN_PROTOCOL, WORKFLOW_PROTOCOL, WORKFLOW_RUN_PROTOCOL, composerType, submissionType, groupType];
|