@deveye/types 0.26.1 → 0.27.1
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/package.json +1 -1
- package/src/domain/featureRegistry.ts +25 -3
- package/src/domain/home.ts +2 -1
- package/src/domain/live.ts +1 -0
- package/src/domain/maintenance.ts +8 -3
- package/src/domain/notifications.ts +3 -1
- package/src/domain/task.test.ts +74 -0
- package/src/domain/task.ts +64 -0
- package/src/domain/workspaceRole.ts +14 -1
- package/src/features/debug.ts +19 -0
- package/src/features/notify.ts +37 -1
- package/src/features/registry.ts +2 -0
- package/src/features/task.ts +44 -0
- package/src/features/workspace.ts +27 -1
- package/src/http/auth.ts +46 -1
- package/src/index.ts +40 -1
- package/src/protocol/agent.ts +39 -0
- package/src/protocol/envelope.test.ts +44 -0
- package/src/protocol/envelope.ts +36 -0
- package/src/sdk/client-ambient.d.ts +154 -12
- package/src/sdk/client.ts +37 -0
- package/src/sdk/manifest.ts +1 -1
- package/src/sdk/providers.ts +168 -38
- package/src/sdk/server.ts +68 -1
- package/src/sdk/testing.test.ts +18 -0
- package/src/sdk/testing.ts +97 -10
package/src/sdk/providers.ts
CHANGED
|
@@ -216,6 +216,21 @@ export interface DatabaseMeasureProvider {
|
|
|
216
216
|
|
|
217
217
|
export const DATABASE_ITEMS_PROVIDER = 'database.items' as const;
|
|
218
218
|
|
|
219
|
+
/** A database at home in one of the workspaces asked for, as `listHome` lists it. */
|
|
220
|
+
export interface DatabaseHomeItem {
|
|
221
|
+
id: number;
|
|
222
|
+
workspaceId: number;
|
|
223
|
+
/** The label the workspace gave it. */
|
|
224
|
+
name: string;
|
|
225
|
+
engine: 'mysql' | 'postgres';
|
|
226
|
+
/** What the server answered to `SELECT VERSION()` at the last check, raw; `null` before any. */
|
|
227
|
+
serverVersion: string | null;
|
|
228
|
+
/** When that check ran, epoch seconds. */
|
|
229
|
+
checkedAt: number | null;
|
|
230
|
+
/** Held over the plan's limit: the feature does not check it, and a consumer leaves it alone too. */
|
|
231
|
+
paused: boolean;
|
|
232
|
+
}
|
|
233
|
+
|
|
219
234
|
export interface DatabaseItemsProvider {
|
|
220
235
|
/** Is this database visible from this workspace: its home, or one it is projected into? */
|
|
221
236
|
exists(databaseId: number, workspaceId: number): Promise<boolean>;
|
|
@@ -227,6 +242,10 @@ export interface DatabaseItemsProvider {
|
|
|
227
242
|
* an id.
|
|
228
243
|
*/
|
|
229
244
|
labelOf(databaseId: number, workspaceId: number): Promise<string | null>;
|
|
245
|
+
/** Every database whose HOME is one of `workspaceIds`, with what its last check saw of the server. */
|
|
246
|
+
listHome(workspaceIds: readonly number[]): Promise<readonly DatabaseHomeItem[]>;
|
|
247
|
+
/** May this member read the database? The rule of the feature's screens, the item's override included. */
|
|
248
|
+
authorize(databaseId: number, workspaceId: number, userId: number): Promise<SdkAccessVerdict>;
|
|
230
249
|
}
|
|
231
250
|
|
|
232
251
|
/**
|
|
@@ -422,8 +441,48 @@ export interface GitHomeRepo {
|
|
|
422
441
|
headSha: string | null;
|
|
423
442
|
/** When that head was read at the forge (epoch seconds): a consumer that cloned later knows better. */
|
|
424
443
|
headSeenAt: number | null;
|
|
444
|
+
/** Held over the plan's limit: Git does not sync it, and a consumer leaves it alone too. */
|
|
445
|
+
paused: boolean;
|
|
425
446
|
}
|
|
426
447
|
|
|
448
|
+
/** What `GitItemsProvider.readFiles` reads, and how much of it. */
|
|
449
|
+
export interface GitFilesRequest {
|
|
450
|
+
/** The commit, branch or tag to read; `null` reads whatever the forge's HEAD points to. */
|
|
451
|
+
ref: string | null;
|
|
452
|
+
/** The tree the caller read last time: when the ref still points to it, no file is fetched. */
|
|
453
|
+
knownTree: string | null;
|
|
454
|
+
/** Which files to read, by their full path in the tree. */
|
|
455
|
+
wants(path: string): boolean;
|
|
456
|
+
/** At most this many files; the rest is reported through `truncated`. */
|
|
457
|
+
maxFiles: number;
|
|
458
|
+
/** A file above this size is not fetched, and is reported through `skipped`. */
|
|
459
|
+
maxFileBytes: number;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
export interface GitRepoFile {
|
|
463
|
+
path: string;
|
|
464
|
+
text: string;
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
export type GitFilesResult =
|
|
468
|
+
| { status: 'unchanged'; tree: string }
|
|
469
|
+
| {
|
|
470
|
+
status: 'ok';
|
|
471
|
+
/** The tree that was read: hand it back as `knownTree` next time. */
|
|
472
|
+
tree: string;
|
|
473
|
+
files: readonly GitRepoFile[];
|
|
474
|
+
/** The forge cut the tree short, or `maxFiles` was reached: some wanted files were never seen. */
|
|
475
|
+
truncated: boolean;
|
|
476
|
+
/** Wanted files left out for their size. */
|
|
477
|
+
skipped: readonly string[];
|
|
478
|
+
}
|
|
479
|
+
| {
|
|
480
|
+
status: 'error';
|
|
481
|
+
error: string;
|
|
482
|
+
/** The failure comes from the repository's side (its token, its rights, its quota), not from the instance. */
|
|
483
|
+
userSide: boolean;
|
|
484
|
+
};
|
|
485
|
+
|
|
427
486
|
/** A repository as read from its home: its name, where it lives, its default branch. */
|
|
428
487
|
export interface GitRepoDescription {
|
|
429
488
|
workspaceId: number;
|
|
@@ -479,58 +538,70 @@ export interface GitItemsProvider {
|
|
|
479
538
|
* the right to read them answers through `error`, never by throwing.
|
|
480
539
|
*/
|
|
481
540
|
activity(repoId: number, workspaceId: number, since: number): Promise<DeployActivity>;
|
|
541
|
+
/**
|
|
542
|
+
* Selected files of the repository, read at the forge without a clone
|
|
543
|
+
* (one request for the tree, one per file kept), with the repository's
|
|
544
|
+
* token when it has one. Sessionless; `null` when the repository is gone
|
|
545
|
+
* from that workspace. A refusal answers through `status: 'error'`,
|
|
546
|
+
* never by throwing.
|
|
547
|
+
*/
|
|
548
|
+
readFiles(
|
|
549
|
+
repoId: number,
|
|
550
|
+
workspaceId: number,
|
|
551
|
+
request: GitFilesRequest
|
|
552
|
+
): Promise<GitFilesResult | null>;
|
|
482
553
|
}
|
|
483
554
|
|
|
484
555
|
/**
|
|
485
|
-
* Key under `FeatureService.providers` for the CVE
|
|
486
|
-
*
|
|
487
|
-
*
|
|
488
|
-
* the
|
|
489
|
-
* to the NVD's quota.
|
|
556
|
+
* Key under `FeatureService.providers` for what the CVE feature's watch finds
|
|
557
|
+
* in one item of another feature: the vulnerable packages and tools of a Git
|
|
558
|
+
* repository, read from the watch's own inventory. Nothing is fetched on the
|
|
559
|
+
* spot: the watch reads the item on its own cadence, and answers from there.
|
|
490
560
|
*/
|
|
491
|
-
export const
|
|
492
|
-
|
|
493
|
-
/** A product as the NVD's CPE names it: `f5` / `nginx`, `nodejs` / `node.js`. */
|
|
494
|
-
export interface CveProductRef {
|
|
495
|
-
vendor: string;
|
|
496
|
-
product: string;
|
|
497
|
-
}
|
|
498
|
-
|
|
499
|
-
export interface CveLookupItem extends CveProductRef {
|
|
500
|
-
/** An exact version (`1.25.3`). A floating one (`1.25`) compares to nothing. */
|
|
501
|
-
version: string;
|
|
502
|
-
}
|
|
561
|
+
export const CVE_EXPOSURE_PROVIDER = 'cve.exposure' as const;
|
|
503
562
|
|
|
504
|
-
|
|
505
|
-
|
|
563
|
+
/** One finding of the watch on an item, with what in the item it touches. */
|
|
564
|
+
export interface CveItemFinding {
|
|
565
|
+
/** The finding's id in the CVE feature. */
|
|
566
|
+
id: number;
|
|
567
|
+
kind: 'vuln' | 'eol' | 'deprecated' | 'outdated';
|
|
568
|
+
/** `ignored`: a member of the workspace decided it does not apply. */
|
|
569
|
+
state: 'open' | 'ignored';
|
|
570
|
+
/** The public id: the CVE when there is one, the advisory otherwise. */
|
|
571
|
+
advisoryId: string | null;
|
|
506
572
|
severity: 'none' | 'low' | 'medium' | 'high' | 'critical';
|
|
507
573
|
score: number | null;
|
|
574
|
+
title: string;
|
|
508
575
|
summary: string;
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
576
|
+
component: { kind: 'package' | 'product'; ecosystem: string; name: string; version: string };
|
|
577
|
+
path: string | null;
|
|
578
|
+
line: number | null;
|
|
579
|
+
/** The versions that fix it, when the advisory names some. */
|
|
580
|
+
fixed: readonly string[];
|
|
581
|
+
reference: string | null;
|
|
513
582
|
}
|
|
514
583
|
|
|
515
|
-
export interface
|
|
516
|
-
/**
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
584
|
+
export interface CveItemExposure {
|
|
585
|
+
/** `pending`: not read yet. `error`: the last read failed, `findings` is what the read before it found. */
|
|
586
|
+
status: 'pending' | 'ok' | 'error' | 'excluded';
|
|
587
|
+
/** The last full read of the item, epoch seconds. */
|
|
588
|
+
scannedAt: number | null;
|
|
589
|
+
/** What the item's source announced at that read (a repository's head commit). */
|
|
590
|
+
revision: string | null;
|
|
591
|
+
error: string | null;
|
|
592
|
+
findings: readonly CveItemFinding[];
|
|
524
593
|
}
|
|
525
594
|
|
|
526
|
-
export interface
|
|
595
|
+
export interface CveExposureProvider {
|
|
527
596
|
/**
|
|
528
|
-
*
|
|
529
|
-
*
|
|
530
|
-
*
|
|
597
|
+
* What the watch holds for this item, `null` when it does not follow it.
|
|
598
|
+
* Sessionless and unfiltered: the caller has checked that its member may
|
|
599
|
+
* see the item.
|
|
531
600
|
*/
|
|
532
|
-
|
|
533
|
-
|
|
601
|
+
ofItem(
|
|
602
|
+
workspaceId: number,
|
|
603
|
+
item: { feature: 'git'; itemId: number }
|
|
604
|
+
): Promise<CveItemExposure | null>;
|
|
534
605
|
}
|
|
535
606
|
|
|
536
607
|
/**
|
|
@@ -548,6 +619,27 @@ export const GIT_CLIENT_PROVIDER = 'git.client' as const;
|
|
|
548
619
|
*/
|
|
549
620
|
export const DEPLOY_ITEMS_PROVIDER = 'deploy.items' as const;
|
|
550
621
|
|
|
622
|
+
/** A deploy target at home in one of the workspaces asked for, as `listHome` lists it. */
|
|
623
|
+
export interface DeployHomeTarget {
|
|
624
|
+
id: number;
|
|
625
|
+
workspaceId: number;
|
|
626
|
+
name: string;
|
|
627
|
+
provider: 'dokploy' | 'github' | 'agent';
|
|
628
|
+
/** Held over the plan's limit: the feature does not sync it, and a consumer leaves it alone too. */
|
|
629
|
+
paused: boolean;
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
/**
|
|
633
|
+
* What a target runs, as its provider describes it: the image it pulls, the
|
|
634
|
+
* compose file it starts. Only what names software, nothing of the
|
|
635
|
+
* provider's own settings (credentials, environment).
|
|
636
|
+
*/
|
|
637
|
+
export interface DeployInventory {
|
|
638
|
+
/** `repository:tag` references. */
|
|
639
|
+
images: readonly string[];
|
|
640
|
+
composeFiles: readonly { path: string; text: string }[];
|
|
641
|
+
}
|
|
642
|
+
|
|
551
643
|
export interface DeployItemsProvider {
|
|
552
644
|
/** Is this target visible from this workspace: its home, or one it is projected into? */
|
|
553
645
|
exists(targetId: number, workspaceId: number): Promise<boolean>;
|
|
@@ -569,6 +661,18 @@ export interface DeployItemsProvider {
|
|
|
569
661
|
* provider first, so a deployment that just ended is not missed.
|
|
570
662
|
*/
|
|
571
663
|
activity(targetId: number, workspaceId: number, since: number): Promise<DeployActivity>;
|
|
664
|
+
/** Every target whose HOME is one of `workspaceIds`. */
|
|
665
|
+
listHome(workspaceIds: readonly number[]): Promise<readonly DeployHomeTarget[]>;
|
|
666
|
+
/**
|
|
667
|
+
* What the target runs, asked of its provider on the spot. `null` for a
|
|
668
|
+
* target the provider cannot describe that way (a workflow, a machine's
|
|
669
|
+
* service: the repository and the device say it already), or one gone.
|
|
670
|
+
* A refusal answers through `error`, never by throwing.
|
|
671
|
+
*/
|
|
672
|
+
inventoryOf(
|
|
673
|
+
targetId: number,
|
|
674
|
+
workspaceId: number
|
|
675
|
+
): Promise<{ inventory: DeployInventory | null; error: string | null } | null>;
|
|
572
676
|
}
|
|
573
677
|
|
|
574
678
|
/** An item of another feature, as a picker lists it. */
|
|
@@ -664,6 +768,32 @@ export interface WhiteboardItemsProvider {
|
|
|
664
768
|
*/
|
|
665
769
|
export const WHITEBOARD_CLIENT_PROVIDER = 'whiteboard.client' as const;
|
|
666
770
|
|
|
771
|
+
/**
|
|
772
|
+
* Key under `FeatureService.providers` for the apps the app's Projects feature
|
|
773
|
+
* links to: same shape and same reason as `UPTIME_ITEMS_PROVIDER`
|
|
774
|
+
* (`ft_projects_appstores_links` is Projects' table).
|
|
775
|
+
*/
|
|
776
|
+
export const APPSTORES_ITEMS_PROVIDER = 'appstores.items' as const;
|
|
777
|
+
|
|
778
|
+
export interface AppStoresItemsProvider {
|
|
779
|
+
/** Is this app visible from this workspace: its home, or one it is projected into? */
|
|
780
|
+
exists(appId: number, workspaceId: number): Promise<boolean>;
|
|
781
|
+
/**
|
|
782
|
+
* The item's display name as seen from `workspaceId` (its home, or a
|
|
783
|
+
* workspace it is projected into): the provider resolves the home and its
|
|
784
|
+
* OPEN cipher itself. `null` when it is gone or unreadable.
|
|
785
|
+
*/
|
|
786
|
+
labelOf(appId: number, workspaceId: number): Promise<string | null>;
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
/**
|
|
790
|
+
* Key under `FeatureClient.providers` for the Stores pieces the app's Projects
|
|
791
|
+
* screens compose: the list of the workspace's apps, a linked app shown inside
|
|
792
|
+
* a project's tab, and the feature's own app dialog.
|
|
793
|
+
* The contract types live in `@deveye/types/sdk/client`.
|
|
794
|
+
*/
|
|
795
|
+
export const APPSTORES_CLIENT_PROVIDER = 'appstores.client' as const;
|
|
796
|
+
|
|
667
797
|
/**
|
|
668
798
|
* Key of what Projects knows about the items of OTHER features: the projects
|
|
669
799
|
* of the workspace that link them (a module's list shows how many projects use
|
package/src/sdk/server.ts
CHANGED
|
@@ -9,6 +9,7 @@ import type { LogLevelName } from '../domain/logs';
|
|
|
9
9
|
import type { ItemAccess } from '../domain/sharing';
|
|
10
10
|
import type { AuthWindow, DeviceReport, IntegrityReport, ReportProcess } from '../domain/report';
|
|
11
11
|
import type { ContainerEngine, DockerAction, DockerInventory } from '../domain/deviceDocker';
|
|
12
|
+
import type { PackageInventory } from '../protocol/agent';
|
|
12
13
|
import type { PathExclusion } from '../domain/pathExclusions';
|
|
13
14
|
import type { AgentManifest } from '../http/device';
|
|
14
15
|
import type { MetricSnapshot } from '../domain/metrics';
|
|
@@ -769,6 +770,11 @@ export interface AgentsFacade {
|
|
|
769
770
|
): Promise<{ ok: boolean; error?: string }>;
|
|
770
771
|
/** The device's containers, images and volumes; `null` when its agent does not answer in time. */
|
|
771
772
|
dockerInventory(deviceId: string, timeoutMs?: number): Promise<DockerInventory | null>;
|
|
773
|
+
/**
|
|
774
|
+
* The system packages installed on the device; `null` when its agent does
|
|
775
|
+
* not answer in time, or is older than `AGENT_PKG_INVENTORY_PROBE`.
|
|
776
|
+
*/
|
|
777
|
+
packageInventory(deviceId: string, timeoutMs?: number): Promise<PackageInventory | null>;
|
|
772
778
|
/**
|
|
773
779
|
* The `.tar.gz` of one of the device's folders, built by its agent and
|
|
774
780
|
* pulled at the consumer's pace: the agent sends no more than the
|
|
@@ -1096,6 +1102,63 @@ export interface SdkSharing {
|
|
|
1096
1102
|
setOrder(itemId: string, order: number): Promise<void>;
|
|
1097
1103
|
}
|
|
1098
1104
|
|
|
1105
|
+
/** What a service says of a long operation when it opens it. */
|
|
1106
|
+
export interface SdkTaskOpening {
|
|
1107
|
+
/** The account the operation runs for: the member who asked for it. Only that account sees it. */
|
|
1108
|
+
userId: number;
|
|
1109
|
+
workspaceId: number;
|
|
1110
|
+
/** What is being done, as a noun phrase: "Sauvegarde", "Conversion". */
|
|
1111
|
+
title: string;
|
|
1112
|
+
/** What it acts on (a name), to tell two operations with the same title apart. */
|
|
1113
|
+
subject?: string;
|
|
1114
|
+
/** The item it targets: "Ouvrir" leads there, and the screens of your feature filter on it. */
|
|
1115
|
+
itemId?: string | number;
|
|
1116
|
+
/**
|
|
1117
|
+
* The kind of operation. With a `key`, opening the same
|
|
1118
|
+
* `(workspaceId, itemId, key)` again for the same account returns the
|
|
1119
|
+
* operation already running instead of a second one: call `open` each
|
|
1120
|
+
* time you find the work again (a queue you poll, a restart).
|
|
1121
|
+
*/
|
|
1122
|
+
key?: string;
|
|
1123
|
+
/**
|
|
1124
|
+
* Offers "Annuler" in the account's area. Called once, when the person
|
|
1125
|
+
* asks; stop the work, then call `cancelled()`.
|
|
1126
|
+
*/
|
|
1127
|
+
cancel?: () => void | Promise<void>;
|
|
1128
|
+
}
|
|
1129
|
+
|
|
1130
|
+
/** A long operation a service runs, see {@link SdkTasks}. Every call after the first ending is ignored. */
|
|
1131
|
+
export interface SdkTask {
|
|
1132
|
+
readonly id: string;
|
|
1133
|
+
/** How far it is: at most four updates a second leave, the last one always. */
|
|
1134
|
+
progress(update: CommandProgress): void;
|
|
1135
|
+
/** It succeeded; `note` is the sentence of the outcome ("Terminé." without it). */
|
|
1136
|
+
done(note?: string): void;
|
|
1137
|
+
/**
|
|
1138
|
+
* It failed; `message` is the sentence to show. `code` decides whether the
|
|
1139
|
+
* person is offered to report it: `internal` (the default) for what the
|
|
1140
|
+
* instance must fix, `conflict` for what their side explains.
|
|
1141
|
+
*/
|
|
1142
|
+
fail(message: string, code?: ErrorCode): void;
|
|
1143
|
+
/** It stopped because the person asked: it leaves the area without an outcome. */
|
|
1144
|
+
cancelled(): void;
|
|
1145
|
+
}
|
|
1146
|
+
|
|
1147
|
+
/**
|
|
1148
|
+
* The operations area of an account, from a service: the work a member
|
|
1149
|
+
* launched and that goes on without their command (a backup, a deployment, a
|
|
1150
|
+
* conversion). The host stamps your feature and shows the operation to that
|
|
1151
|
+
* account only, in every tab, once it has checked the account still reads
|
|
1152
|
+
* your feature in that workspace; a restart of the server forgets it, so open
|
|
1153
|
+
* it again when your service resumes the work.
|
|
1154
|
+
*
|
|
1155
|
+
* Not for what runs on its own (a schedule, a reconciliation): nobody asked
|
|
1156
|
+
* for it, so it belongs to your screens, not to someone's area.
|
|
1157
|
+
*/
|
|
1158
|
+
export interface SdkTasks {
|
|
1159
|
+
open(opening: SdkTaskOpening): SdkTask;
|
|
1160
|
+
}
|
|
1161
|
+
|
|
1099
1162
|
/** Live invalidation from a background service, which writes without a command. */
|
|
1100
1163
|
export interface SdkLive {
|
|
1101
1164
|
/**
|
|
@@ -1327,7 +1390,9 @@ export interface SdkFeatureContext<Repo = unknown> {
|
|
|
1327
1390
|
* Report how far this command has got, to its caller only. Best effort, at
|
|
1328
1391
|
* most 4 frames a second (the last one always goes out); each frame re-arms
|
|
1329
1392
|
* the caller's timeout, so a long command that keeps reporting never times
|
|
1330
|
-
* out. Steps are user-facing sentences
|
|
1393
|
+
* out. Steps are user-facing sentences: when the caller runs the command
|
|
1394
|
+
* as a long operation (`runTask`), they also show in the operations area of
|
|
1395
|
+
* the account, in every tab.
|
|
1331
1396
|
*/
|
|
1332
1397
|
progress(update: CommandProgress): void;
|
|
1333
1398
|
/** The server key derivations, the same `keys` a service gets. */
|
|
@@ -1713,6 +1778,8 @@ export interface FeatureServiceDeps<Repo = unknown> {
|
|
|
1713
1778
|
geo: SdkGeo;
|
|
1714
1779
|
/** Live invalidation of your feature's resources, from a service. */
|
|
1715
1780
|
live: SdkLive;
|
|
1781
|
+
/** The long operations a member launched and your service runs. */
|
|
1782
|
+
tasks: SdkTasks;
|
|
1716
1783
|
/**
|
|
1717
1784
|
* Audit line recorded as the SYSTEM (no session). `userId` attributes the
|
|
1718
1785
|
* line to a user when the work concerns their data.
|
package/src/sdk/testing.test.ts
CHANGED
|
@@ -39,6 +39,24 @@ test('createTestContext follows the manifest for extras, and records what handle
|
|
|
39
39
|
assert.equal(member.store.rows.get('k')?.mode, 'private');
|
|
40
40
|
});
|
|
41
41
|
|
|
42
|
+
test('notify refuses, as the host does, a manifest without the capability', async () => {
|
|
43
|
+
const manifest = { nativeCapabilities: ['members.read' as const] };
|
|
44
|
+
const ctx = createTestContext({ manifest });
|
|
45
|
+
await assert.rejects(ctx.deveye.notify.send({ subject: 's', body: 'b' }), {
|
|
46
|
+
code: 'forbidden'
|
|
47
|
+
});
|
|
48
|
+
await assert.rejects(ctx.deveye.notify.hasRoute(), { code: 'forbidden' });
|
|
49
|
+
assert.deepEqual(ctx.recorded.notifications, []);
|
|
50
|
+
|
|
51
|
+
const deps = createTestServiceDeps({ manifest });
|
|
52
|
+
await assert.rejects(deps.deveyeFor(1).notify.send({ subject: 's', body: 'b' }), {
|
|
53
|
+
code: 'forbidden'
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
const declared = createTestContext({ manifest: { nativeCapabilities: ['notify' as const] } });
|
|
57
|
+
assert.equal(await declared.deveye.notify.send({ subject: 's', body: 'b' }), true);
|
|
58
|
+
});
|
|
59
|
+
|
|
42
60
|
test('createTestServiceDeps: one store per workspace, hand-driven tickers, a wrapper that round-trips', async () => {
|
|
43
61
|
const deps = createTestServiceDeps({
|
|
44
62
|
workspaceIds: [1, 2],
|
package/src/sdk/testing.ts
CHANGED
|
@@ -3,11 +3,12 @@ import os from 'node:os';
|
|
|
3
3
|
import path from 'node:path';
|
|
4
4
|
|
|
5
5
|
import type { ZodType } from 'zod';
|
|
6
|
+
import type { ErrorCode } from '../protocol/error';
|
|
6
7
|
import { DEFAULT_METRIC_INTERVAL_SECONDS } from '../domain/device';
|
|
7
8
|
import type { ItemAccess } from '../domain/sharing';
|
|
8
9
|
import type { PathExclusion } from '../domain/pathExclusions';
|
|
9
10
|
import { normaliseDomainHost } from './domains';
|
|
10
|
-
import { resolveExtras, type FeatureManifest } from './manifest';
|
|
11
|
+
import { resolveExtras, type FeatureManifest, type NativeCapability } from './manifest';
|
|
11
12
|
import {
|
|
12
13
|
FeatureError,
|
|
13
14
|
type AgentFolderArchive,
|
|
@@ -33,6 +34,7 @@ import {
|
|
|
33
34
|
type SdkPlanPauses,
|
|
34
35
|
type SdkProviders,
|
|
35
36
|
type SdkQuota,
|
|
37
|
+
type SdkTaskOpening,
|
|
36
38
|
type SdkTelemetry,
|
|
37
39
|
type SdkTelemetrySnapshot,
|
|
38
40
|
type StorageEncryption,
|
|
@@ -162,12 +164,26 @@ function recordingNotify(
|
|
|
162
164
|
recorded: RecordedCalls,
|
|
163
165
|
hasRoute: boolean,
|
|
164
166
|
accepted: boolean,
|
|
165
|
-
liveChannels: readonly number[]
|
|
167
|
+
liveChannels: readonly number[],
|
|
168
|
+
capabilities: readonly NativeCapability[] | undefined
|
|
166
169
|
): DevEyeFacade['notify'] {
|
|
167
170
|
let posted = 0;
|
|
171
|
+
// The host refuses every notify call of a module that did not declare the
|
|
172
|
+
// capability: a test that hands its manifest gets the same refusal.
|
|
173
|
+
const refused = (): Promise<never> | null =>
|
|
174
|
+
capabilities && !capabilities.includes('notify')
|
|
175
|
+
? Promise.reject(
|
|
176
|
+
new FeatureError(
|
|
177
|
+
'forbidden',
|
|
178
|
+
"Declare 'notify' in the manifest's nativeCapabilities"
|
|
179
|
+
)
|
|
180
|
+
)
|
|
181
|
+
: null;
|
|
168
182
|
return {
|
|
169
|
-
hasRoute: () => Promise.resolve(hasRoute),
|
|
183
|
+
hasRoute: () => refused() ?? Promise.resolve(hasRoute),
|
|
170
184
|
send(alert, opts) {
|
|
185
|
+
const refusal = refused();
|
|
186
|
+
if (refusal) return refusal;
|
|
171
187
|
recorded.notifications.push({
|
|
172
188
|
subject: alert.subject,
|
|
173
189
|
body: alert.body,
|
|
@@ -182,8 +198,10 @@ function recordingNotify(
|
|
|
182
198
|
// answers false, so a test sees what the module does with it.
|
|
183
199
|
return Promise.resolve(accepted);
|
|
184
200
|
},
|
|
185
|
-
liveChannels: () => Promise.resolve(liveChannels.map((id) => ({ id }))),
|
|
201
|
+
liveChannels: () => refused() ?? Promise.resolve(liveChannels.map((id) => ({ id }))),
|
|
186
202
|
postLive(channelId, message, messageId) {
|
|
203
|
+
const refusal = refused();
|
|
204
|
+
if (refusal) return refusal;
|
|
187
205
|
recorded.liveMessages.push({
|
|
188
206
|
channelId,
|
|
189
207
|
messageId: messageId ?? null,
|
|
@@ -259,6 +277,7 @@ export interface TestFolderArchive {
|
|
|
259
277
|
interface TestAgents {
|
|
260
278
|
dockerRun?: DevEyeFacade['agents']['dockerRun'];
|
|
261
279
|
dockerInventory?: DevEyeFacade['agents']['dockerInventory'];
|
|
280
|
+
packageInventory?: DevEyeFacade['agents']['packageInventory'];
|
|
262
281
|
/** By device id. A device without an entry answers nothing: its stream throws. */
|
|
263
282
|
archives?: Readonly<Record<string, TestFolderArchive | Error>>;
|
|
264
283
|
openTcp?: DevEyeFacade['agents']['openTcp'];
|
|
@@ -352,6 +371,10 @@ function recordingAgents(recorded: RecordedCalls, docker: TestAgents = {}): DevE
|
|
|
352
371
|
docker.dockerInventory
|
|
353
372
|
? docker.dockerInventory(deviceId, timeoutMs)
|
|
354
373
|
: Promise.resolve(null),
|
|
374
|
+
packageInventory: (deviceId, timeoutMs) =>
|
|
375
|
+
docker.packageInventory
|
|
376
|
+
? docker.packageInventory(deviceId, timeoutMs)
|
|
377
|
+
: Promise.resolve(null),
|
|
355
378
|
archiveFolder(deviceId, request) {
|
|
356
379
|
recorded.agentRequests.push({ method: 'archiveFolder', deviceId });
|
|
357
380
|
recorded.archiveRequests.push({ deviceId, ...request });
|
|
@@ -484,10 +507,12 @@ export interface TestContextOverrides<Repo> {
|
|
|
484
507
|
extras?: Record<string, boolean | string>;
|
|
485
508
|
/**
|
|
486
509
|
* Your manifest: `canExtra` / `extraValue` then follow the exact runtime
|
|
487
|
-
* rules ({@link resolveExtras})
|
|
488
|
-
*
|
|
510
|
+
* rules ({@link resolveExtras}), and `deveye.notify` refuses, as the host
|
|
511
|
+
* does, a module that did not declare `'notify'`. Without it no extra is
|
|
512
|
+
* declared, so every key answers `false` / `''`, owner or not, and no
|
|
513
|
+
* capability is checked.
|
|
489
514
|
*/
|
|
490
|
-
manifest?: Pick<FeatureManifest, 'extraPermissions'>;
|
|
515
|
+
manifest?: Pick<FeatureManifest, 'extraPermissions' | 'nativeCapabilities'>;
|
|
491
516
|
/** What `deveye.notify.hasRoute` answers. Default true. */
|
|
492
517
|
hasRoute?: boolean;
|
|
493
518
|
/** What `deveye.notify.send` resolves (no usable channel: false). Default true; recorded either way. */
|
|
@@ -507,6 +532,8 @@ export interface TestContextOverrides<Repo> {
|
|
|
507
532
|
dockerRun?: DevEyeFacade['agents']['dockerRun'];
|
|
508
533
|
/** What `deveye.agents.dockerInventory` resolves. Default null (no answer). */
|
|
509
534
|
dockerInventory?: DevEyeFacade['agents']['dockerInventory'];
|
|
535
|
+
/** What `deveye.agents.packageInventory` resolves. Default null (no answer). */
|
|
536
|
+
packageInventory?: DevEyeFacade['agents']['packageInventory'];
|
|
510
537
|
/** What `deveye.agents.archiveFolder` streams, by device id. Default none: the stream throws. */
|
|
511
538
|
archives?: Readonly<Record<string, TestFolderArchive | Error>>;
|
|
512
539
|
/** What `deveye.agents.openTcp` resolves. Default a rejection, as for an offline device. */
|
|
@@ -565,7 +592,8 @@ export function createTestContext<Repo = undefined>(
|
|
|
565
592
|
recorded,
|
|
566
593
|
overrides.hasRoute ?? true,
|
|
567
594
|
overrides.notifyAccepted ?? true,
|
|
568
|
-
overrides.liveChannels ?? []
|
|
595
|
+
overrides.liveChannels ?? [],
|
|
596
|
+
overrides.manifest?.nativeCapabilities
|
|
569
597
|
),
|
|
570
598
|
mail: { listAccounts: () => Promise.resolve([]) },
|
|
571
599
|
members: {
|
|
@@ -792,6 +820,21 @@ export interface RecordedServiceCalls extends RecordedCalls {
|
|
|
792
820
|
mails: { userId: number; to: string; message: SdkAccountMailMessage }[];
|
|
793
821
|
/** What `alertInstance` raised, in order, keys as the module gave them. */
|
|
794
822
|
instanceAlerts: SdkInstanceAlert[];
|
|
823
|
+
/** The operations opened through `tasks.open`, in order, each with what the service then said of it. */
|
|
824
|
+
tasks: RecordedTask[];
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
/** One operation a service opened, as a test reads it. `cancel` is the callback it gave: call it to play "Annuler". */
|
|
828
|
+
export interface RecordedTask extends Omit<SdkTaskOpening, 'cancel'> {
|
|
829
|
+
id: string;
|
|
830
|
+
cancel?: () => void | Promise<void>;
|
|
831
|
+
updates: CommandProgress[];
|
|
832
|
+
/** `null` while it runs. */
|
|
833
|
+
outcome:
|
|
834
|
+
| { state: 'done'; note?: string }
|
|
835
|
+
| { state: 'failed'; message: string; code: ErrorCode }
|
|
836
|
+
| { state: 'cancelled' }
|
|
837
|
+
| null;
|
|
795
838
|
}
|
|
796
839
|
|
|
797
840
|
export interface TestServiceDeps<Repo> extends FeatureServiceDeps<Repo> {
|
|
@@ -802,6 +845,8 @@ export interface TestServiceDeps<Repo> extends FeatureServiceDeps<Repo> {
|
|
|
802
845
|
|
|
803
846
|
export interface TestServiceOverrides<Repo> {
|
|
804
847
|
repo?: Repo;
|
|
848
|
+
/** Your manifest: `deveyeFor(...).notify` then refuses, as the host does, a module that did not declare `'notify'`. */
|
|
849
|
+
manifest?: Pick<FeatureManifest, 'nativeCapabilities'>;
|
|
805
850
|
/** What `listWorkspaceIds` answers. Default `[1]`. */
|
|
806
851
|
workspaceIds?: readonly number[];
|
|
807
852
|
/** Devices every workspace reveals. Default none. */
|
|
@@ -812,6 +857,8 @@ export interface TestServiceOverrides<Repo> {
|
|
|
812
857
|
dockerRun?: DevEyeFacade['agents']['dockerRun'];
|
|
813
858
|
/** What `deps.agents.dockerInventory` resolves. Default null (no answer). */
|
|
814
859
|
dockerInventory?: DevEyeFacade['agents']['dockerInventory'];
|
|
860
|
+
/** What `deps.agents.packageInventory` resolves. Default null (no answer). */
|
|
861
|
+
packageInventory?: DevEyeFacade['agents']['packageInventory'];
|
|
815
862
|
/** What `deps.agents.archiveFolder` streams, by device id. Default none: the stream throws. */
|
|
816
863
|
archives?: Readonly<Record<string, TestFolderArchive | Error>>;
|
|
817
864
|
/** What `deps.agents.openTcp` resolves. Default a rejection, as for an offline device. */
|
|
@@ -874,7 +921,8 @@ export function createTestServiceDeps<Repo = undefined>(
|
|
|
874
921
|
liveChanges: [],
|
|
875
922
|
liveTopicChanges: [],
|
|
876
923
|
mails: [],
|
|
877
|
-
instanceAlerts: []
|
|
924
|
+
instanceAlerts: [],
|
|
925
|
+
tasks: []
|
|
878
926
|
};
|
|
879
927
|
const stores = new Map<number, TestFeatureStore>();
|
|
880
928
|
const sealedBytes = new Map<string, Uint8Array>();
|
|
@@ -882,7 +930,8 @@ export function createTestServiceDeps<Repo = undefined>(
|
|
|
882
930
|
recorded,
|
|
883
931
|
overrides.hasRoute ?? true,
|
|
884
932
|
overrides.notifyAccepted ?? true,
|
|
885
|
-
overrides.liveChannels ?? []
|
|
933
|
+
overrides.liveChannels ?? [],
|
|
934
|
+
overrides.manifest?.nativeCapabilities
|
|
886
935
|
);
|
|
887
936
|
const devices = recordingDevices(overrides.devices ?? []);
|
|
888
937
|
const pauses = memoryPauses(overrides.pausedItems);
|
|
@@ -946,6 +995,44 @@ export function createTestServiceDeps<Repo = undefined>(
|
|
|
946
995
|
country: (ip) => overrides.countries?.[ip] ?? null,
|
|
947
996
|
attribution: { text: 'IP Geolocation by DB-IP', url: 'https://db-ip.com' }
|
|
948
997
|
},
|
|
998
|
+
tasks: {
|
|
999
|
+
open(opening) {
|
|
1000
|
+
// The host's rule: the same keyed work, found again while it runs, is the same operation.
|
|
1001
|
+
const running =
|
|
1002
|
+
opening.key === undefined
|
|
1003
|
+
? undefined
|
|
1004
|
+
: recorded.tasks.find(
|
|
1005
|
+
(t) =>
|
|
1006
|
+
t.outcome === null &&
|
|
1007
|
+
t.userId === opening.userId &&
|
|
1008
|
+
t.workspaceId === opening.workspaceId &&
|
|
1009
|
+
t.key === opening.key &&
|
|
1010
|
+
String(t.itemId ?? '') === String(opening.itemId ?? '')
|
|
1011
|
+
);
|
|
1012
|
+
const task: RecordedTask = running ?? {
|
|
1013
|
+
...opening,
|
|
1014
|
+
id: `task-${recorded.tasks.length + 1}`,
|
|
1015
|
+
updates: [],
|
|
1016
|
+
outcome: null
|
|
1017
|
+
};
|
|
1018
|
+
if (running) running.cancel = opening.cancel;
|
|
1019
|
+
else recorded.tasks.push(task);
|
|
1020
|
+
const settle = (outcome: NonNullable<RecordedTask['outcome']>): void => {
|
|
1021
|
+
task.outcome ??= outcome;
|
|
1022
|
+
};
|
|
1023
|
+
return {
|
|
1024
|
+
id: task.id,
|
|
1025
|
+
progress: (update) => {
|
|
1026
|
+
if (task.outcome === null) task.updates.push(update);
|
|
1027
|
+
},
|
|
1028
|
+
done: (note) =>
|
|
1029
|
+
settle(note === undefined ? { state: 'done' } : { state: 'done', note }),
|
|
1030
|
+
fail: (message, code = 'internal') =>
|
|
1031
|
+
settle({ state: 'failed', message, code }),
|
|
1032
|
+
cancelled: () => settle({ state: 'cancelled' })
|
|
1033
|
+
};
|
|
1034
|
+
}
|
|
1035
|
+
},
|
|
949
1036
|
live: {
|
|
950
1037
|
changed(workspaceId, topics) {
|
|
951
1038
|
recorded.liveChanges.push(workspaceId);
|