@milaboratories/pl-mcp-server 53.1.0 → 55.0.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/README.md +30 -37
- package/dist/_virtual/_rolldown/runtime.cjs +22 -0
- package/dist/agent-tables.cjs +116 -0
- package/dist/agent-tables.cjs.map +1 -0
- package/dist/agent-tables.js +115 -0
- package/dist/agent-tables.js.map +1 -0
- package/dist/instructions.cjs +16 -12
- package/dist/instructions.cjs.map +1 -1
- package/dist/instructions.js +16 -12
- package/dist/instructions.js.map +1 -1
- package/dist/package.cjs +1 -1
- package/dist/package.js +1 -1
- package/dist/project-holds.cjs +122 -0
- package/dist/project-holds.cjs.map +1 -0
- package/dist/project-holds.js +122 -0
- package/dist/project-holds.js.map +1 -0
- package/dist/project-list.cjs +16 -0
- package/dist/project-list.cjs.map +1 -0
- package/dist/project-list.js +16 -0
- package/dist/project-list.js.map +1 -0
- package/dist/server-log.cjs +48 -18
- package/dist/server-log.cjs.map +1 -1
- package/dist/server-log.js +48 -19
- package/dist/server-log.js.map +1 -1
- package/dist/server.cjs +71 -18
- package/dist/server.cjs.map +1 -1
- package/dist/server.d.ts +37 -24
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +71 -19
- package/dist/server.js.map +1 -1
- package/dist/tools/agent-tables.cjs +424 -0
- package/dist/tools/agent-tables.cjs.map +1 -0
- package/dist/tools/agent-tables.js +417 -0
- package/dist/tools/agent-tables.js.map +1 -0
- package/dist/tools/await.cjs +216 -72
- package/dist/tools/await.cjs.map +1 -1
- package/dist/tools/await.js +212 -74
- package/dist/tools/await.js.map +1 -1
- package/dist/tools/block-action-context.cjs +14 -9
- package/dist/tools/block-action-context.cjs.map +1 -1
- package/dist/tools/block-action-context.js +14 -9
- package/dist/tools/block-action-context.js.map +1 -1
- package/dist/tools/block-api.cjs +17 -2
- package/dist/tools/block-api.cjs.map +1 -1
- package/dist/tools/block-api.js +17 -3
- package/dist/tools/block-api.js.map +1 -1
- package/dist/tools/block-params.cjs +275 -6
- package/dist/tools/block-params.cjs.map +1 -1
- package/dist/tools/block-params.js +273 -8
- package/dist/tools/block-params.js.map +1 -1
- package/dist/tools/block-state.cjs +343 -57
- package/dist/tools/block-state.cjs.map +1 -1
- package/dist/tools/block-state.js +337 -61
- package/dist/tools/block-state.js.map +1 -1
- package/dist/tools/block-storage.cjs +153 -0
- package/dist/tools/block-storage.cjs.map +1 -0
- package/dist/tools/block-storage.js +147 -0
- package/dist/tools/block-storage.js.map +1 -0
- package/dist/tools/block-updates.cjs +405 -0
- package/dist/tools/block-updates.cjs.map +1 -0
- package/dist/tools/block-updates.js +397 -0
- package/dist/tools/block-updates.js.map +1 -0
- package/dist/tools/blocks.cjs +199 -76
- package/dist/tools/blocks.cjs.map +1 -1
- package/dist/tools/blocks.js +193 -76
- package/dist/tools/blocks.js.map +1 -1
- package/dist/tools/data-query.cjs +470 -101
- package/dist/tools/data-query.cjs.map +1 -1
- package/dist/tools/data-query.js +465 -106
- package/dist/tools/data-query.js.map +1 -1
- package/dist/tools/errors.cjs +21 -0
- package/dist/tools/errors.cjs.map +1 -0
- package/dist/tools/errors.js +20 -0
- package/dist/tools/errors.js.map +1 -0
- package/dist/tools/files.cjs +643 -0
- package/dist/tools/files.cjs.map +1 -0
- package/dist/tools/files.js +613 -0
- package/dist/tools/files.js.map +1 -0
- package/dist/tools/ids.cjs +72 -0
- package/dist/tools/ids.cjs.map +1 -1
- package/dist/tools/ids.js +63 -2
- package/dist/tools/ids.js.map +1 -1
- package/dist/tools/imports.cjs +195 -0
- package/dist/tools/imports.cjs.map +1 -0
- package/dist/tools/imports.js +186 -0
- package/dist/tools/imports.js.map +1 -0
- package/dist/tools/logs.cjs +111 -30
- package/dist/tools/logs.cjs.map +1 -1
- package/dist/tools/logs.js +108 -31
- package/dist/tools/logs.js.map +1 -1
- package/dist/tools/paging.cjs +27 -0
- package/dist/tools/paging.cjs.map +1 -0
- package/dist/tools/paging.js +26 -0
- package/dist/tools/paging.js.map +1 -0
- package/dist/tools/params-merge.cjs +106 -0
- package/dist/tools/params-merge.cjs.map +1 -0
- package/dist/tools/params-merge.js +106 -0
- package/dist/tools/params-merge.js.map +1 -0
- package/dist/tools/project-template.cjs +75 -0
- package/dist/tools/project-template.cjs.map +1 -0
- package/dist/tools/project-template.js +72 -0
- package/dist/tools/project-template.js.map +1 -0
- package/dist/tools/projects.cjs +68 -38
- package/dist/tools/projects.cjs.map +1 -1
- package/dist/tools/projects.js +69 -40
- package/dist/tools/projects.js.map +1 -1
- package/dist/tools/readiness.cjs +104 -0
- package/dist/tools/readiness.cjs.map +1 -0
- package/dist/tools/readiness.js +99 -0
- package/dist/tools/readiness.js.map +1 -0
- package/dist/tools/result-pool.cjs +260 -0
- package/dist/tools/result-pool.cjs.map +1 -0
- package/dist/tools/result-pool.js +252 -0
- package/dist/tools/result-pool.js.map +1 -0
- package/dist/tools/sandbox.cjs +112 -14
- package/dist/tools/sandbox.cjs.map +1 -1
- package/dist/tools/sandbox.js +110 -15
- package/dist/tools/sandbox.js.map +1 -1
- package/dist/tools/sdk-types.cjs +24 -0
- package/dist/tools/sdk-types.cjs.map +1 -0
- package/dist/tools/sdk-types.js +24 -0
- package/dist/tools/sdk-types.js.map +1 -0
- package/dist/tools/settle.cjs +155 -0
- package/dist/tools/settle.cjs.map +1 -0
- package/dist/tools/settle.js +148 -0
- package/dist/tools/settle.js.map +1 -0
- package/dist/tools/tokens.cjs +49 -8
- package/dist/tools/tokens.cjs.map +1 -1
- package/dist/tools/tokens.js +45 -9
- package/dist/tools/tokens.js.map +1 -1
- package/dist/tools/types.cjs +62 -11
- package/dist/tools/types.cjs.map +1 -1
- package/dist/tools/types.js +60 -12
- package/dist/tools/types.js.map +1 -1
- package/dist/tools/value-outline.cjs +119 -0
- package/dist/tools/value-outline.cjs.map +1 -0
- package/dist/tools/value-outline.js +117 -0
- package/dist/tools/value-outline.js.map +1 -0
- package/package.json +9 -8
- package/src/agent-tables.test.ts +246 -0
- package/src/agent-tables.ts +145 -0
- package/src/instructions.ts +16 -12
- package/src/project-holds.test.ts +362 -0
- package/src/project-holds.ts +207 -0
- package/src/project-list.test.ts +38 -0
- package/src/project-list.ts +25 -0
- package/src/server-log.test.ts +71 -6
- package/src/server-log.ts +58 -27
- package/src/server.logging.test.ts +46 -51
- package/src/server.test.ts +166 -22
- package/src/server.ts +97 -37
- package/src/tools/agent-tables.test.ts +650 -0
- package/src/tools/agent-tables.ts +673 -0
- package/src/tools/await.test.ts +287 -0
- package/src/tools/await.ts +393 -122
- package/src/tools/block-action-context.ts +17 -13
- package/src/tools/block-api.ts +19 -4
- package/src/tools/block-params.test.ts +828 -0
- package/src/tools/block-params.ts +626 -10
- package/src/tools/block-state.test.ts +480 -0
- package/src/tools/block-state.ts +678 -83
- package/src/tools/block-storage.test.ts +270 -0
- package/src/tools/block-storage.ts +195 -0
- package/src/tools/block-updates.test.ts +605 -0
- package/src/tools/block-updates.ts +751 -0
- package/src/tools/blocks.test.ts +169 -22
- package/src/tools/blocks.ts +354 -113
- package/src/tools/data-query.test.ts +641 -0
- package/src/tools/data-query.ts +839 -145
- package/src/tools/errors.test.ts +33 -0
- package/src/tools/errors.ts +23 -0
- package/src/tools/files.test.ts +842 -0
- package/src/tools/files.ts +990 -0
- package/src/tools/ids.test.ts +87 -0
- package/src/tools/ids.ts +89 -1
- package/src/tools/imports.ts +304 -0
- package/src/tools/logs.test.ts +70 -0
- package/src/tools/logs.ts +154 -44
- package/src/tools/paging.test.ts +39 -0
- package/src/tools/paging.ts +39 -0
- package/src/tools/params-merge.test.ts +274 -0
- package/src/tools/params-merge.ts +149 -0
- package/src/tools/project-template.test.ts +230 -0
- package/src/tools/project-template.ts +135 -0
- package/src/tools/projects.test.ts +122 -0
- package/src/tools/projects.ts +136 -48
- package/src/tools/readiness.test.ts +195 -0
- package/src/tools/readiness.ts +189 -0
- package/src/tools/result-pool.test.ts +377 -0
- package/src/tools/result-pool.ts +483 -0
- package/src/tools/sandbox.test.ts +128 -10
- package/src/tools/sandbox.ts +132 -18
- package/src/tools/sdk-types.ts +54 -0
- package/src/tools/settle.test.ts +340 -0
- package/src/tools/settle.ts +312 -0
- package/src/tools/tokens.ts +74 -9
- package/src/tools/types.ts +84 -12
- package/src/tools/value-outline.ts +143 -0
- package/dist/tools/screenshot.cjs +0 -44
- package/dist/tools/screenshot.cjs.map +0 -1
- package/dist/tools/screenshot.js +0 -44
- package/dist/tools/screenshot.js.map +0 -1
- package/dist/tools/ui-interaction.cjs +0 -140
- package/dist/tools/ui-interaction.cjs.map +0 -1
- package/dist/tools/ui-interaction.js +0 -140
- package/dist/tools/ui-interaction.js.map +0 -1
- package/src/tools/screenshot.ts +0 -61
- package/src/tools/ui-interaction.ts +0 -184
|
@@ -0,0 +1,828 @@
|
|
|
1
|
+
import { describe, expect, test } from "vitest";
|
|
2
|
+
import { asBlockId, type BlockCodeFeatureFlags } from "@milaboratories/pl-model-common";
|
|
3
|
+
import { ModelAPIVersionMismatchError } from "@milaboratories/pl-errors";
|
|
4
|
+
import type {
|
|
5
|
+
BlockPackSpecAny,
|
|
6
|
+
BlockParamsModel,
|
|
7
|
+
DescribedBlockPack,
|
|
8
|
+
VersionedBlockData,
|
|
9
|
+
} from "@milaboratories/pl-middle-layer";
|
|
10
|
+
import { isPlainObject } from "@milaboratories/helpers";
|
|
11
|
+
import type {
|
|
12
|
+
BlockParamsProject,
|
|
13
|
+
GetBlockContractDeps,
|
|
14
|
+
GetBlockContractInput,
|
|
15
|
+
SetBlockParamsInput,
|
|
16
|
+
} from "./block-params";
|
|
17
|
+
import {
|
|
18
|
+
blockParamsResult,
|
|
19
|
+
DECLARATION_CAP_BYTES,
|
|
20
|
+
getBlockContract,
|
|
21
|
+
writeBlockParams,
|
|
22
|
+
} from "./block-params";
|
|
23
|
+
import { SDK_TYPES } from "./sdk-types";
|
|
24
|
+
import type { BlockKindReference } from "@platforma-sdk/model";
|
|
25
|
+
import type { WriteOutcome } from "./block-state";
|
|
26
|
+
import { UNSUPPORTED_BLOCK_API_TEXT } from "./block-api";
|
|
27
|
+
|
|
28
|
+
const kind = "@platforma-open/org.numbers.kind@1.0.0" as BlockKindReference;
|
|
29
|
+
const resolvedSpec: BlockPackSpecAny = {
|
|
30
|
+
type: "from-registry-v2",
|
|
31
|
+
registryUrl: "https://registry.test/",
|
|
32
|
+
id: { organization: "org", name: "numbers", version: "1.2.3" },
|
|
33
|
+
};
|
|
34
|
+
const contractUrl = "https://registry.test/kinds/org/numbers/1.0.0/contract.d.ts";
|
|
35
|
+
const kindDtsUrl = "https://registry.test/kinds/org/numbers/1.0.0/kind.d.ts";
|
|
36
|
+
const builtDts =
|
|
37
|
+
'import { PlRef } from "@platforma-sdk/model";\n' +
|
|
38
|
+
"/** Numbers to sum. */\n" +
|
|
39
|
+
"type BlockParams = { numbers?: number[]; source?: PlRef };\n" +
|
|
40
|
+
'export declare const kind: import("@platforma-sdk/block-kind").CompiledBlockKind<BlockParams>;\n';
|
|
41
|
+
|
|
42
|
+
/** An error with the name the registry reader gives its read failures. */
|
|
43
|
+
function registryReadError(message: string): Error {
|
|
44
|
+
const e = new Error(message);
|
|
45
|
+
e.name = "RegistryReadError";
|
|
46
|
+
return e;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function deps(
|
|
50
|
+
described: () => Promise<DescribedBlockPack>,
|
|
51
|
+
resolve: () => Promise<BlockPackSpecAny> = async () => resolvedSpec,
|
|
52
|
+
) {
|
|
53
|
+
const describedSpecs: BlockPackSpecAny[] = [];
|
|
54
|
+
const value: GetBlockContractDeps = {
|
|
55
|
+
resolveKind: async () => await resolve(),
|
|
56
|
+
describeBlockPack: async (spec) => {
|
|
57
|
+
describedSpecs.push(spec);
|
|
58
|
+
return await described();
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
return { deps: value, described: describedSpecs };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const byKind: GetBlockContractInput = { spec: { type: "kind", kind } };
|
|
65
|
+
|
|
66
|
+
function resultOf(result: Awaited<ReturnType<typeof getBlockContract>>) {
|
|
67
|
+
return { isError: "isError" in result, text: result.content[0].text };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
describe("get_block_contract", () => {
|
|
71
|
+
test("returns the contract of the kind the resolved block implements, whole", async () => {
|
|
72
|
+
const { deps: d, described } = deps(async () => ({
|
|
73
|
+
kind,
|
|
74
|
+
declaredKind: kind,
|
|
75
|
+
declaration: { text: builtDts, url: contractUrl },
|
|
76
|
+
}));
|
|
77
|
+
|
|
78
|
+
const result = resultOf(await getBlockContract(d, byKind));
|
|
79
|
+
|
|
80
|
+
expect(described).toEqual([resolvedSpec]);
|
|
81
|
+
expect(result.isError).toBe(false);
|
|
82
|
+
expect(JSON.parse(result.text)).toEqual({
|
|
83
|
+
kind,
|
|
84
|
+
source: "contract.d.ts",
|
|
85
|
+
declaration: builtDts,
|
|
86
|
+
sdkTypes: SDK_TYPES,
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test("short declarations of id types say which form each takes and which tool gives it", () => {
|
|
91
|
+
const types = SDK_TYPES;
|
|
92
|
+
expect(types.ColumnValueId).toContain("copy from `valueIds` of get_column_values");
|
|
93
|
+
expect(types.ColumnValueId).toContain("never build it");
|
|
94
|
+
expect(types.ColumnValueId).toContain("of a pool column too");
|
|
95
|
+
expect(types.GlobalPObjectId).toContain("list_columns and get_block_outputs");
|
|
96
|
+
expect(types.GlobalPObjectId).toContain("list_pool_columns one with a `ref`");
|
|
97
|
+
expect(types.PObjectId).toContain("A leaf column id only");
|
|
98
|
+
expect(types.PObjectId).toContain("never a filtered, discovered or overridden id");
|
|
99
|
+
expect(types.PlRef).toContain("Object form of a leaf GlobalPObjectId");
|
|
100
|
+
expect(types.PlRef).toContain("or from list_pool_columns");
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
test("ColumnUniversalId says block workflows take leaf ids and where to copy one", () => {
|
|
104
|
+
const types = SDK_TYPES;
|
|
105
|
+
expect(types.ColumnUniversalId).toContain("Block workflows resolve leaf ids only today");
|
|
106
|
+
expect(types.ColumnUniversalId).toContain("the id the block offers in its option outputs");
|
|
107
|
+
expect(types.ColumnUniversalId).not.toContain("discovered");
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
test("DatasetSelection declares each enrichment's id and label and says where to copy one", () => {
|
|
111
|
+
const types = SDK_TYPES;
|
|
112
|
+
expect(types.DatasetSelection).toContain(
|
|
113
|
+
"readonly enrichments?: readonly { readonly id: ColumnUniversalId; readonly label: string }[]",
|
|
114
|
+
);
|
|
115
|
+
expect(types.DatasetSelection).not.toContain("unknown[]");
|
|
116
|
+
expect(types.DatasetSelection).toContain("copy each from `enrichments`");
|
|
117
|
+
expect(types.DatasetSelection).toContain("never build one");
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
test("a declaration over the cap is its size and URL, not its text", async () => {
|
|
121
|
+
const big = builtDts + `/** ${"x".repeat(DECLARATION_CAP_BYTES)} */\n`;
|
|
122
|
+
const { deps: d } = deps(async () => ({
|
|
123
|
+
kind,
|
|
124
|
+
declaredKind: kind,
|
|
125
|
+
declaration: { text: big, url: contractUrl },
|
|
126
|
+
}));
|
|
127
|
+
|
|
128
|
+
const result = JSON.parse(resultOf(await getBlockContract(d, byKind)).text);
|
|
129
|
+
|
|
130
|
+
expect(result).toEqual({
|
|
131
|
+
kind,
|
|
132
|
+
source: "contract.d.ts",
|
|
133
|
+
declarationBytes: Buffer.byteLength(big),
|
|
134
|
+
declarationUrl: contractUrl,
|
|
135
|
+
notes: [
|
|
136
|
+
`The declaration is ${Buffer.byteLength(big)} bytes, over the ${DECLARATION_CAP_BYTES} ` +
|
|
137
|
+
"this tool returns. Read it at `declarationUrl`.",
|
|
138
|
+
],
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
test("a kind published before declarations is a note pointing to its file and the block", async () => {
|
|
143
|
+
const { deps: d } = deps(async () => ({
|
|
144
|
+
kind,
|
|
145
|
+
declaredKind: kind,
|
|
146
|
+
legacyDeclarationUrl: kindDtsUrl,
|
|
147
|
+
homepage: "https://github.com/org/numbers",
|
|
148
|
+
}));
|
|
149
|
+
|
|
150
|
+
const result = JSON.parse(resultOf(await getBlockContract(d, byKind)).text);
|
|
151
|
+
|
|
152
|
+
expect(result).toEqual({
|
|
153
|
+
kind,
|
|
154
|
+
declarationUrl: kindDtsUrl,
|
|
155
|
+
homepage: "https://github.com/org/numbers",
|
|
156
|
+
notes: [
|
|
157
|
+
`Kind ${kind} has no contract.d.ts: it was built before kinds carried a declaration ` +
|
|
158
|
+
"agents can read. One comes with the next release of the block. `declarationUrl` is " +
|
|
159
|
+
"its kind.d.ts, with every dependency inlined (tens of KB); the params are the type " +
|
|
160
|
+
"argument of `kind`. The block's homepage is `homepage`.",
|
|
161
|
+
],
|
|
162
|
+
});
|
|
163
|
+
expect(result).not.toHaveProperty("declaration");
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
test("a dev block reads the kind package it depends on", async () => {
|
|
167
|
+
const newer = "@platforma-open/org.numbers.kind@1.1.0" as BlockKindReference;
|
|
168
|
+
const fileUrl = "file:///blocks/numbers/node_modules/kind/dist/contract.d.ts";
|
|
169
|
+
const legacyUrl = "file:///blocks/numbers/node_modules/kind/dist/kind.d.ts";
|
|
170
|
+
const built = deps(async () => ({
|
|
171
|
+
kind,
|
|
172
|
+
declaredKind: newer,
|
|
173
|
+
declaration: { text: builtDts, url: fileUrl },
|
|
174
|
+
}));
|
|
175
|
+
const dev: GetBlockContractInput = { spec: { type: "dev-v2", folder: "/blocks/numbers" } };
|
|
176
|
+
|
|
177
|
+
const result = JSON.parse(resultOf(await getBlockContract(built.deps, dev)).text);
|
|
178
|
+
|
|
179
|
+
expect(built.described).toEqual([dev.spec]);
|
|
180
|
+
expect(result).toMatchObject({
|
|
181
|
+
kind,
|
|
182
|
+
declaredKind: newer,
|
|
183
|
+
source: "contract.d.ts",
|
|
184
|
+
declaration: builtDts,
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
// A dev kind package built before declarations is rebuilt, not released.
|
|
188
|
+
const legacy = JSON.parse(
|
|
189
|
+
resultOf(
|
|
190
|
+
await getBlockContract(
|
|
191
|
+
deps(async () => ({
|
|
192
|
+
kind,
|
|
193
|
+
declaredKind: kind,
|
|
194
|
+
legacyDeclarationUrl: legacyUrl,
|
|
195
|
+
})).deps,
|
|
196
|
+
dev,
|
|
197
|
+
),
|
|
198
|
+
).text,
|
|
199
|
+
);
|
|
200
|
+
expect(legacy).toMatchObject({ kind, declarationUrl: legacyUrl });
|
|
201
|
+
expect(legacy).not.toHaveProperty("homepage");
|
|
202
|
+
expect(legacy.notes[0]).toContain(
|
|
203
|
+
"Rebuilding the kind package with a current @milaboratories/ts-builder adds one.",
|
|
204
|
+
);
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
test("an unknown kind and an unreadable registry are told apart", async () => {
|
|
208
|
+
const missing = resultOf(
|
|
209
|
+
await getBlockContract(
|
|
210
|
+
deps(
|
|
211
|
+
async () => {
|
|
212
|
+
throw new Error("unreachable");
|
|
213
|
+
},
|
|
214
|
+
async () => {
|
|
215
|
+
throw new Error("no-matching-kind-version");
|
|
216
|
+
},
|
|
217
|
+
).deps,
|
|
218
|
+
byKind,
|
|
219
|
+
),
|
|
220
|
+
);
|
|
221
|
+
expect(missing.isError).toBe(true);
|
|
222
|
+
expect(missing.text).toContain(`Kind "${kind}" was not resolved to a block`);
|
|
223
|
+
expect(missing.text).toContain("Check the kind name and version.");
|
|
224
|
+
|
|
225
|
+
const outage = resultOf(
|
|
226
|
+
await getBlockContract(
|
|
227
|
+
deps(
|
|
228
|
+
async () => {
|
|
229
|
+
throw new Error("unreachable");
|
|
230
|
+
},
|
|
231
|
+
async () => {
|
|
232
|
+
throw registryReadError("Reading the overview failed: HTTP 503");
|
|
233
|
+
},
|
|
234
|
+
).deps,
|
|
235
|
+
byKind,
|
|
236
|
+
),
|
|
237
|
+
);
|
|
238
|
+
expect(outage.isError).toBe(true);
|
|
239
|
+
expect(outage.text).toContain("Reading the overview failed: HTTP 503");
|
|
240
|
+
expect(outage.text).toContain("The registry could not be read");
|
|
241
|
+
expect(outage.text).not.toContain("Check the kind name");
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
test("a kind file that cannot be read is an error naming the registry", async () => {
|
|
245
|
+
const result = resultOf(
|
|
246
|
+
await getBlockContract(
|
|
247
|
+
deps(async () => {
|
|
248
|
+
throw registryReadError("Reading contract.d.ts failed: HTTP 500");
|
|
249
|
+
}).deps,
|
|
250
|
+
byKind,
|
|
251
|
+
),
|
|
252
|
+
);
|
|
253
|
+
expect(result.isError).toBe(true);
|
|
254
|
+
expect(result.text).toContain("The block was not read: Reading contract.d.ts failed: HTTP 500");
|
|
255
|
+
expect(result.text).toContain("The registry could not be read");
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
test("a block without a kind, or a kind without declarations, is a note", async () => {
|
|
259
|
+
const noKind = JSON.parse(
|
|
260
|
+
resultOf(await getBlockContract(deps(async () => ({})).deps, byKind)).text,
|
|
261
|
+
);
|
|
262
|
+
expect(noKind).toEqual({
|
|
263
|
+
notes: ["This block declares no kind, so add_block takes no params for it."],
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
const noFiles = JSON.parse(
|
|
267
|
+
resultOf(
|
|
268
|
+
await getBlockContract(deps(async () => ({ kind, declaredKind: kind })).deps, byKind),
|
|
269
|
+
).text,
|
|
270
|
+
);
|
|
271
|
+
expect(noFiles).toEqual({
|
|
272
|
+
kind,
|
|
273
|
+
notes: [
|
|
274
|
+
`Kind ${kind} has no contract.d.ts: it was built before kinds carried a declaration ` +
|
|
275
|
+
"agents can read. One comes with the next release of the block.",
|
|
276
|
+
],
|
|
277
|
+
});
|
|
278
|
+
});
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
describe("get_block_params", () => {
|
|
282
|
+
const initialization = { kind, params: { numbers: [1, 2, 3], title: "Sum" } };
|
|
283
|
+
|
|
284
|
+
test("without transform, returns the kind and the params", async () => {
|
|
285
|
+
const result = await blockParamsResult(initialization, undefined, 5000);
|
|
286
|
+
expect("isError" in result).toBe(false);
|
|
287
|
+
expect(JSON.parse(result.content[0].text)).toEqual(initialization);
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
test("transform reads part of the params, and the kind", async () => {
|
|
291
|
+
const part = await blockParamsResult(initialization, "params.numbers", 5000);
|
|
292
|
+
expect(JSON.parse(part.content[0].text)).toEqual([1, 2, 3]);
|
|
293
|
+
|
|
294
|
+
const both = await blockParamsResult(initialization, "({ kind, title: params.title })", 5000);
|
|
295
|
+
expect(JSON.parse(both.content[0].text)).toEqual({ kind, title: "Sum" });
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
test("a failed transform is an error result with a hint", async () => {
|
|
299
|
+
const result = await blockParamsResult(initialization, "params.numbers.foo.bar", 5000);
|
|
300
|
+
expect("isError" in result && result.isError).toBe(true);
|
|
301
|
+
expect(result.content[0].text).toContain("Transform failed: TypeError");
|
|
302
|
+
expect(result.content[0].text).toContain("Available variables: kind, params.");
|
|
303
|
+
});
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
type Payload = { operation: string; value: unknown };
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* The data of the block in these tests: a threshold and groups the params drive, a file preset
|
|
310
|
+
* the params cannot express, resources the kind accepts and the params leave out, and view state.
|
|
311
|
+
*/
|
|
312
|
+
type Data = {
|
|
313
|
+
threshold: unknown;
|
|
314
|
+
groups: { id: string; label: string; color: string }[];
|
|
315
|
+
preset: { type: "name"; name: string } | { type: "file"; file: string };
|
|
316
|
+
memGB?: number;
|
|
317
|
+
view: { sort: string };
|
|
318
|
+
};
|
|
319
|
+
|
|
320
|
+
function data(overrides: Partial<Data> = {}): Data {
|
|
321
|
+
return {
|
|
322
|
+
threshold: 1,
|
|
323
|
+
groups: [
|
|
324
|
+
{ id: "g1", label: "Group 1", color: "red" },
|
|
325
|
+
{ id: "g2", label: "Group 2", color: "blue" },
|
|
326
|
+
],
|
|
327
|
+
preset: { type: "name", name: "default" },
|
|
328
|
+
view: { sort: "asc" },
|
|
329
|
+
...overrides,
|
|
330
|
+
};
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/** Raw block storage of a BlockModelV3 block, in the format the SDK writes. */
|
|
334
|
+
function storage(value: unknown, dataVersion = "v2") {
|
|
335
|
+
return {
|
|
336
|
+
__pl_a7f3e2b9__: "v1",
|
|
337
|
+
__dataVersion: dataVersion,
|
|
338
|
+
__data: value,
|
|
339
|
+
__pluginRegistry: {},
|
|
340
|
+
__plugins: {},
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/** `templateParams` of the test block. */
|
|
345
|
+
function templateParams(d: unknown): unknown {
|
|
346
|
+
if (!isPlainObject(d) || !Array.isArray(d.groups) || !isPlainObject(d.preset))
|
|
347
|
+
throw new Error("not the test block's data");
|
|
348
|
+
return {
|
|
349
|
+
threshold: d.threshold,
|
|
350
|
+
groups: d.groups.map((g: unknown) => (isPlainObject(g) ? { id: g.id, label: g.label } : g)),
|
|
351
|
+
...(d.preset.type === "name" ? { preset: d.preset } : {}),
|
|
352
|
+
};
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/** `init` of the test block, data version v2; the kind accepts numeric thresholds only. */
|
|
356
|
+
function init(p: unknown): VersionedBlockData {
|
|
357
|
+
if (!isPlainObject(p) || typeof p.threshold !== "number" || !Array.isArray(p.groups))
|
|
358
|
+
throw new Error("threshold must be a number");
|
|
359
|
+
return {
|
|
360
|
+
dataVersion: "v2",
|
|
361
|
+
data: {
|
|
362
|
+
threshold: p.threshold,
|
|
363
|
+
groups: p.groups.map((g: unknown) => (isPlainObject(g) ? { ...g, color: "auto" } : g)),
|
|
364
|
+
preset: p.preset ?? { type: "name", name: "default" },
|
|
365
|
+
...(p.memGB !== undefined ? { memGB: p.memGB } : {}),
|
|
366
|
+
view: { sort: "none" },
|
|
367
|
+
},
|
|
368
|
+
};
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/** `ResultOrError` of a model call that may throw. */
|
|
372
|
+
function attempt<T>(fn: () => T): { value: T; error?: undefined } | { error: Error } {
|
|
373
|
+
try {
|
|
374
|
+
return { value: fn() };
|
|
375
|
+
} catch (e: unknown) {
|
|
376
|
+
return { error: e instanceof Error ? e : new Error(String(e)) };
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
const testModel: BlockParamsModel = {
|
|
381
|
+
givesParams: true,
|
|
382
|
+
appliesParams: false,
|
|
383
|
+
applyParams: () => ({ error: new Error("this block does not apply params") }),
|
|
384
|
+
paramsOf: ({ data: d }) => {
|
|
385
|
+
try {
|
|
386
|
+
return { value: templateParams(d) };
|
|
387
|
+
} catch (e: unknown) {
|
|
388
|
+
return { error: e instanceof Error ? e : new Error(String(e)) };
|
|
389
|
+
}
|
|
390
|
+
},
|
|
391
|
+
dataOf: (p) => {
|
|
392
|
+
try {
|
|
393
|
+
return { value: init(p) };
|
|
394
|
+
} catch (e: unknown) {
|
|
395
|
+
return { error: e instanceof Error ? e : new Error(String(e)) };
|
|
396
|
+
}
|
|
397
|
+
},
|
|
398
|
+
};
|
|
399
|
+
|
|
400
|
+
const V3_FLAGS: BlockCodeFeatureFlags = { requiresModelAPIVersion: 2 };
|
|
401
|
+
|
|
402
|
+
/** A project with one block `b1` whose storage is `blockStorage`. It records the storage writes. */
|
|
403
|
+
function stubProject(
|
|
404
|
+
blockStorage: unknown,
|
|
405
|
+
options: {
|
|
406
|
+
mutate?: (payload: Payload) => void;
|
|
407
|
+
featureFlags?: BlockCodeFeatureFlags;
|
|
408
|
+
model?: () => Promise<BlockParamsModel>;
|
|
409
|
+
} = {},
|
|
410
|
+
) {
|
|
411
|
+
const writes: Payload[] = [];
|
|
412
|
+
const project: BlockParamsProject = {
|
|
413
|
+
overview: {
|
|
414
|
+
getValue: async () => ({
|
|
415
|
+
blocks: [{ id: "b1", featureFlags: options.featureFlags ?? V3_FLAGS }],
|
|
416
|
+
}),
|
|
417
|
+
},
|
|
418
|
+
getBlockState: () => ({ getValue: async () => ({ blockStorage }) }),
|
|
419
|
+
mutateBlockStorage: async (_blockId, payload) => {
|
|
420
|
+
options.mutate?.(payload);
|
|
421
|
+
writes.push(payload);
|
|
422
|
+
},
|
|
423
|
+
getBlockParamsModel: options.model ?? (async () => testModel),
|
|
424
|
+
};
|
|
425
|
+
return { project, writes };
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
const author = { authorId: "test", localVersion: 1 };
|
|
429
|
+
|
|
430
|
+
function input(
|
|
431
|
+
overrides: Partial<SetBlockParamsInput>,
|
|
432
|
+
): Omit<SetBlockParamsInput, "projectId" | "wait" | "timeout"> {
|
|
433
|
+
return { blockId: asBlockId("b1"), ...overrides };
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/** Whether a written outcome left the data unchanged; throws for a refused one. */
|
|
437
|
+
function unchanged(outcome: WriteOutcome): boolean {
|
|
438
|
+
if (outcome.kind !== "written") throw new Error(outcome.result.content[0].text);
|
|
439
|
+
return outcome.unchanged;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
/** The error text of a refused write. */
|
|
443
|
+
function errorText(outcome: WriteOutcome): string {
|
|
444
|
+
if (outcome.kind !== "refused") throw new Error("The write was not refused");
|
|
445
|
+
return outcome.result.content[0].text;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
/** The current params of the test block over `d`. */
|
|
449
|
+
function paramsOf(d: Data) {
|
|
450
|
+
const p = templateParams(d);
|
|
451
|
+
if (!isPlainObject(p)) throw new Error("params are an object");
|
|
452
|
+
return p;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
/**
|
|
456
|
+
* `applyParams` of the test block: the params onto live data, the rest kept. A list element keeps
|
|
457
|
+
* its colour by `id`, a file preset stays when the params name none.
|
|
458
|
+
*/
|
|
459
|
+
function applyParamsOf(d: Data, p: unknown): Data {
|
|
460
|
+
const fresh = dataOfTest(init(p).data);
|
|
461
|
+
if (!isPlainObject(p)) throw new Error("not the test block's params");
|
|
462
|
+
const colours = new Map(d.groups.map((g) => [g.id, g.color]));
|
|
463
|
+
return {
|
|
464
|
+
...d,
|
|
465
|
+
threshold: fresh.threshold,
|
|
466
|
+
groups: fresh.groups.map((g) => ({ ...g, color: colours.get(g.id) ?? g.color })),
|
|
467
|
+
preset: isPlainObject(p.preset) ? fresh.preset : d.preset,
|
|
468
|
+
...(fresh.memGB !== undefined ? { memGB: fresh.memGB } : {}),
|
|
469
|
+
};
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
/** The test block with `applyParams`; `dataOf` is `applyParams(defaults, params)`. */
|
|
473
|
+
function applyingModel(apply: (d: Data, p: unknown) => Data = applyParamsOf): {
|
|
474
|
+
model: BlockParamsModel;
|
|
475
|
+
calls: { dataOf: number; applyParams: number };
|
|
476
|
+
} {
|
|
477
|
+
const calls = { dataOf: 0, applyParams: 0 };
|
|
478
|
+
const model: BlockParamsModel = {
|
|
479
|
+
givesParams: true,
|
|
480
|
+
appliesParams: true,
|
|
481
|
+
paramsOf: testModel.paramsOf,
|
|
482
|
+
dataOf: (p) => {
|
|
483
|
+
calls.dataOf++;
|
|
484
|
+
return attempt(() => ({ dataVersion: "v2", data: apply(defaultData(), p) }));
|
|
485
|
+
},
|
|
486
|
+
applyParams: ({ dataVersion, data: d }, p) => {
|
|
487
|
+
calls.applyParams++;
|
|
488
|
+
if (dataVersion !== "v2") return { error: new Error("the data waits for a migration") };
|
|
489
|
+
return attempt(() => ({ dataVersion: "v2", data: apply(dataOfTest(d), p) }));
|
|
490
|
+
},
|
|
491
|
+
};
|
|
492
|
+
return { model, calls };
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
/** `init` of the test block without params, as data. */
|
|
496
|
+
function defaultData(): Data {
|
|
497
|
+
return data({ groups: [], view: { sort: "none" } });
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
/** `d` as the test block's data, or a throw. */
|
|
501
|
+
function dataOfTest(d: unknown): Data {
|
|
502
|
+
if (
|
|
503
|
+
!isPlainObject(d) ||
|
|
504
|
+
!Array.isArray(d.groups) ||
|
|
505
|
+
!isPlainObject(d.preset) ||
|
|
506
|
+
!isPlainObject(d.view)
|
|
507
|
+
)
|
|
508
|
+
throw new Error("not the test block's data");
|
|
509
|
+
return data({
|
|
510
|
+
threshold: d.threshold,
|
|
511
|
+
groups: d.groups.filter(
|
|
512
|
+
(g: unknown): g is Data["groups"][number] =>
|
|
513
|
+
isPlainObject(g) &&
|
|
514
|
+
typeof g.id === "string" &&
|
|
515
|
+
typeof g.label === "string" &&
|
|
516
|
+
typeof g.color === "string",
|
|
517
|
+
),
|
|
518
|
+
preset:
|
|
519
|
+
d.preset.type === "file" && typeof d.preset.file === "string"
|
|
520
|
+
? { type: "file", file: d.preset.file }
|
|
521
|
+
: { type: "name", name: typeof d.preset.name === "string" ? d.preset.name : "" },
|
|
522
|
+
view: { sort: typeof d.view.sort === "string" ? d.view.sort : "" },
|
|
523
|
+
...(typeof d.memGB === "number" ? { memGB: d.memGB } : {}),
|
|
524
|
+
});
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
describe("set_block_params", () => {
|
|
528
|
+
test("writes what the params changed and keeps the rest", async () => {
|
|
529
|
+
const { project, writes } = stubProject(storage(data()));
|
|
530
|
+
const p1 = {
|
|
531
|
+
...paramsOf(data()),
|
|
532
|
+
threshold: 2,
|
|
533
|
+
groups: [
|
|
534
|
+
{ id: "g2", label: "Second" },
|
|
535
|
+
{ id: "g1", label: "Group 1" },
|
|
536
|
+
],
|
|
537
|
+
};
|
|
538
|
+
const result = await writeBlockParams(project, author, input({ params: p1 }));
|
|
539
|
+
expect(unchanged(result)).toBe(false);
|
|
540
|
+
expect(writes).toEqual([
|
|
541
|
+
{
|
|
542
|
+
operation: "update-block-data",
|
|
543
|
+
value: data({
|
|
544
|
+
threshold: 2,
|
|
545
|
+
groups: [
|
|
546
|
+
{ id: "g2", label: "Second", color: "blue" },
|
|
547
|
+
{ id: "g1", label: "Group 1", color: "red" },
|
|
548
|
+
],
|
|
549
|
+
}),
|
|
550
|
+
},
|
|
551
|
+
]);
|
|
552
|
+
});
|
|
553
|
+
|
|
554
|
+
test("an update expression computes the new params from the current ones", async () => {
|
|
555
|
+
const { project, writes } = stubProject(storage(data({ threshold: 3 })));
|
|
556
|
+
const result = await writeBlockParams(
|
|
557
|
+
project,
|
|
558
|
+
author,
|
|
559
|
+
input({ update: "({ ...params, threshold: params.threshold * 2 })" }),
|
|
560
|
+
);
|
|
561
|
+
expect(unchanged(result)).toBe(false);
|
|
562
|
+
expect(writes[0].value).toEqual(data({ threshold: 6 }));
|
|
563
|
+
});
|
|
564
|
+
|
|
565
|
+
test("an update expression that changes `params` in place writes the change", async () => {
|
|
566
|
+
const { project, writes } = stubProject(storage(data({ threshold: 3 })));
|
|
567
|
+
const result = await writeBlockParams(
|
|
568
|
+
project,
|
|
569
|
+
author,
|
|
570
|
+
input({ update: "(() => { params.threshold = 6; return params; })()" }),
|
|
571
|
+
);
|
|
572
|
+
expect(unchanged(result)).toBe(false);
|
|
573
|
+
expect(writes[0].value).toEqual(data({ threshold: 6 }));
|
|
574
|
+
});
|
|
575
|
+
|
|
576
|
+
test("params equal to the current ones write nothing", async () => {
|
|
577
|
+
const { project, writes } = stubProject(storage(data()));
|
|
578
|
+
const result = await writeBlockParams(project, author, input({ params: paramsOf(data()) }));
|
|
579
|
+
expect(unchanged(result)).toBe(true);
|
|
580
|
+
expect(writes).toEqual([]);
|
|
581
|
+
});
|
|
582
|
+
|
|
583
|
+
test("a key the kind accepts and the params leave out is written, and not reported", async () => {
|
|
584
|
+
const { project, writes } = stubProject(storage(data()));
|
|
585
|
+
const result = await writeBlockParams(
|
|
586
|
+
project,
|
|
587
|
+
author,
|
|
588
|
+
input({ params: { ...paramsOf(data()), memGB: 8 } }),
|
|
589
|
+
);
|
|
590
|
+
expect(unchanged(result)).toBe(false);
|
|
591
|
+
expect(writes[0].value).toEqual(data({ memGB: 8 }));
|
|
592
|
+
});
|
|
593
|
+
|
|
594
|
+
test("params and update together, or neither, are refused", async () => {
|
|
595
|
+
const { project, writes } = stubProject(storage(data()));
|
|
596
|
+
expect(errorText(await writeBlockParams(project, author, input({})))).toContain(
|
|
597
|
+
"Give either `params` or `update`",
|
|
598
|
+
);
|
|
599
|
+
expect(
|
|
600
|
+
errorText(await writeBlockParams(project, author, input({ params: {}, update: "params" }))),
|
|
601
|
+
).toContain("Give either `params` or `update`");
|
|
602
|
+
expect(writes).toEqual([]);
|
|
603
|
+
});
|
|
604
|
+
|
|
605
|
+
test("params the block rejects are refused in the block's words", async () => {
|
|
606
|
+
const { project, writes } = stubProject(storage(data()));
|
|
607
|
+
const text = errorText(
|
|
608
|
+
await writeBlockParams(
|
|
609
|
+
project,
|
|
610
|
+
author,
|
|
611
|
+
input({ params: { ...paramsOf(data()), threshold: "high" } }),
|
|
612
|
+
),
|
|
613
|
+
);
|
|
614
|
+
expect(text).toContain("threshold must be a number");
|
|
615
|
+
expect(text).toContain("Nothing was changed");
|
|
616
|
+
expect(writes).toEqual([]);
|
|
617
|
+
});
|
|
618
|
+
|
|
619
|
+
test("a merge conflict is refused, naming the path and set_block_data", async () => {
|
|
620
|
+
// The file preset is something the params cannot express; another named preset replaces it
|
|
621
|
+
// where base, theirs and ours all differ.
|
|
622
|
+
const ours = data({ preset: { type: "file", file: "preset.json" } });
|
|
623
|
+
const { project, writes } = stubProject(storage(ours));
|
|
624
|
+
const text = errorText(
|
|
625
|
+
await writeBlockParams(
|
|
626
|
+
project,
|
|
627
|
+
author,
|
|
628
|
+
input({ params: { ...paramsOf(ours), preset: { type: "name", name: "other" } } }),
|
|
629
|
+
),
|
|
630
|
+
);
|
|
631
|
+
expect(text).toContain("preset.name");
|
|
632
|
+
expect(text).toContain("set_block_data");
|
|
633
|
+
expect(writes).toEqual([]);
|
|
634
|
+
});
|
|
635
|
+
|
|
636
|
+
test("merged data that would not give the params back is refused", async () => {
|
|
637
|
+
// Naming the default preset leaves base and theirs equal, so the merge keeps the file preset,
|
|
638
|
+
// which the new params do not describe.
|
|
639
|
+
const ours = data({ preset: { type: "file", file: "preset.json" } });
|
|
640
|
+
const { project, writes } = stubProject(storage(ours));
|
|
641
|
+
const text = errorText(
|
|
642
|
+
await writeBlockParams(
|
|
643
|
+
project,
|
|
644
|
+
author,
|
|
645
|
+
input({ params: { ...paramsOf(ours), preset: { type: "name", name: "default" } } }),
|
|
646
|
+
),
|
|
647
|
+
);
|
|
648
|
+
expect(text).toContain("would not give these params back");
|
|
649
|
+
expect(text).toContain("preset");
|
|
650
|
+
expect(text).toContain("set_block_data");
|
|
651
|
+
expect(writes).toEqual([]);
|
|
652
|
+
});
|
|
653
|
+
|
|
654
|
+
test("data at another version than the model builds is refused", async () => {
|
|
655
|
+
const { project, writes } = stubProject(storage(data(), "v1"));
|
|
656
|
+
const text = errorText(
|
|
657
|
+
await writeBlockParams(project, author, input({ params: paramsOf(data()) })),
|
|
658
|
+
);
|
|
659
|
+
expect(text).toContain("waits for a migration");
|
|
660
|
+
expect(text).toContain("set_block_data");
|
|
661
|
+
expect(writes).toEqual([]);
|
|
662
|
+
});
|
|
663
|
+
|
|
664
|
+
test("a block that gives no params is refused", async () => {
|
|
665
|
+
const { project, writes } = stubProject(storage(data()), {
|
|
666
|
+
model: async () => ({ ...testModel, givesParams: false }),
|
|
667
|
+
});
|
|
668
|
+
const text = errorText(await writeBlockParams(project, author, input({ params: {} })));
|
|
669
|
+
expect(text).toContain("gives no params");
|
|
670
|
+
expect(text).toContain("set_block_data");
|
|
671
|
+
expect(writes).toEqual([]);
|
|
672
|
+
});
|
|
673
|
+
|
|
674
|
+
test("a block on block API 1 gets the shared error before its model is asked", async () => {
|
|
675
|
+
let asked = false;
|
|
676
|
+
const { project, writes } = stubProject(
|
|
677
|
+
{ args: {}, uiState: {} },
|
|
678
|
+
{
|
|
679
|
+
featureFlags: {},
|
|
680
|
+
model: async () => {
|
|
681
|
+
asked = true;
|
|
682
|
+
return testModel;
|
|
683
|
+
},
|
|
684
|
+
},
|
|
685
|
+
);
|
|
686
|
+
const result = await writeBlockParams(project, author, input({ params: {} }));
|
|
687
|
+
expect(errorText(result)).toBe(UNSUPPORTED_BLOCK_API_TEXT);
|
|
688
|
+
expect(asked).toBe(false);
|
|
689
|
+
expect(writes).toEqual([]);
|
|
690
|
+
});
|
|
691
|
+
|
|
692
|
+
test("a block on block API 1, refused by the middle layer, gets the shared error", async () => {
|
|
693
|
+
const { project, writes } = stubProject(storage(data()), {
|
|
694
|
+
model: async () => {
|
|
695
|
+
throw new ModelAPIVersionMismatchError("b1", 2, 1);
|
|
696
|
+
},
|
|
697
|
+
});
|
|
698
|
+
const result = await writeBlockParams(project, author, input({ params: paramsOf(data()) }));
|
|
699
|
+
expect(errorText(result)).toBe(UNSUPPORTED_BLOCK_API_TEXT);
|
|
700
|
+
expect(writes).toEqual([]);
|
|
701
|
+
});
|
|
702
|
+
|
|
703
|
+
test("an unknown block is refused", async () => {
|
|
704
|
+
const { project } = stubProject(storage(data()));
|
|
705
|
+
const result = await writeBlockParams(
|
|
706
|
+
project,
|
|
707
|
+
author,
|
|
708
|
+
input({ blockId: asBlockId("nope"), params: {} }),
|
|
709
|
+
);
|
|
710
|
+
expect(errorText(result)).toContain("Block nope not found");
|
|
711
|
+
});
|
|
712
|
+
});
|
|
713
|
+
|
|
714
|
+
describe("set_block_params with a block that declares applyParams", () => {
|
|
715
|
+
test("the block puts the params onto its data and keeps the rest", async () => {
|
|
716
|
+
const ours = data({ memGB: 4, view: { sort: "desc" } });
|
|
717
|
+
const { model, calls } = applyingModel();
|
|
718
|
+
const { project, writes } = stubProject(storage(ours), { model: async () => model });
|
|
719
|
+
const p1 = { ...paramsOf(ours), threshold: 2, groups: [{ id: "g2", label: "Second" }] };
|
|
720
|
+
const result = await writeBlockParams(project, author, input({ params: p1 }));
|
|
721
|
+
expect(unchanged(result)).toBe(false);
|
|
722
|
+
expect(calls.applyParams).toBe(1);
|
|
723
|
+
expect(writes).toEqual([
|
|
724
|
+
{
|
|
725
|
+
operation: "update-block-data",
|
|
726
|
+
value: { ...ours, threshold: 2, groups: [{ id: "g2", label: "Second", color: "blue" }] },
|
|
727
|
+
},
|
|
728
|
+
]);
|
|
729
|
+
});
|
|
730
|
+
|
|
731
|
+
test("an update expression runs over the current params", async () => {
|
|
732
|
+
const { model } = applyingModel();
|
|
733
|
+
const { project, writes } = stubProject(storage(data({ threshold: 3 })), {
|
|
734
|
+
model: async () => model,
|
|
735
|
+
});
|
|
736
|
+
const result = await writeBlockParams(
|
|
737
|
+
project,
|
|
738
|
+
author,
|
|
739
|
+
input({ update: "({ ...params, threshold: params.threshold * 2 })" }),
|
|
740
|
+
);
|
|
741
|
+
expect(unchanged(result)).toBe(false);
|
|
742
|
+
expect(writes[0].value).toEqual(data({ threshold: 6 }));
|
|
743
|
+
});
|
|
744
|
+
|
|
745
|
+
test("a block's own params write nothing", async () => {
|
|
746
|
+
const { model } = applyingModel();
|
|
747
|
+
const { project, writes } = stubProject(storage(data()), { model: async () => model });
|
|
748
|
+
const result = await writeBlockParams(project, author, input({ params: paramsOf(data()) }));
|
|
749
|
+
expect(unchanged(result)).toBe(true);
|
|
750
|
+
expect(writes).toEqual([]);
|
|
751
|
+
});
|
|
752
|
+
|
|
753
|
+
test("an applyParams that does not give its params back is refused", async () => {
|
|
754
|
+
// This block keeps the old threshold when the data is sorted descending.
|
|
755
|
+
const { model } = applyingModel((d, p) => {
|
|
756
|
+
const next = applyParamsOf(d, p);
|
|
757
|
+
return d.view.sort === "desc" ? { ...next, threshold: d.threshold } : next;
|
|
758
|
+
});
|
|
759
|
+
const ours = data({ view: { sort: "desc" } });
|
|
760
|
+
const { project, writes } = stubProject(storage(ours), { model: async () => model });
|
|
761
|
+
const text = errorText(
|
|
762
|
+
await writeBlockParams(
|
|
763
|
+
project,
|
|
764
|
+
author,
|
|
765
|
+
input({ params: { ...paramsOf(ours), threshold: 9 } }),
|
|
766
|
+
),
|
|
767
|
+
);
|
|
768
|
+
expect(text).toContain("applyParams does not give its params back");
|
|
769
|
+
expect(text).toContain("threshold");
|
|
770
|
+
expect(text).toContain("Nothing was changed");
|
|
771
|
+
expect(writes).toEqual([]);
|
|
772
|
+
});
|
|
773
|
+
|
|
774
|
+
test("params the block rejects are refused in the block's words", async () => {
|
|
775
|
+
const { model } = applyingModel();
|
|
776
|
+
const { project, writes } = stubProject(storage(data()), { model: async () => model });
|
|
777
|
+
const text = errorText(
|
|
778
|
+
await writeBlockParams(
|
|
779
|
+
project,
|
|
780
|
+
author,
|
|
781
|
+
input({ params: { ...paramsOf(data()), threshold: "high" } }),
|
|
782
|
+
),
|
|
783
|
+
);
|
|
784
|
+
expect(text).toContain("threshold must be a number");
|
|
785
|
+
expect(writes).toEqual([]);
|
|
786
|
+
});
|
|
787
|
+
|
|
788
|
+
test("an applyParams that fails on the current data is refused", async () => {
|
|
789
|
+
const { model } = applyingModel((d, p) => {
|
|
790
|
+
if (d.view.sort === "desc") throw new Error("cannot read this view");
|
|
791
|
+
return applyParamsOf(d, p);
|
|
792
|
+
});
|
|
793
|
+
const { project, writes } = stubProject(storage(data({ view: { sort: "desc" } })), {
|
|
794
|
+
model: async () => model,
|
|
795
|
+
});
|
|
796
|
+
const text = errorText(
|
|
797
|
+
await writeBlockParams(project, author, input({ params: paramsOf(data()) })),
|
|
798
|
+
);
|
|
799
|
+
expect(text).toContain("applyParams could not put these params onto its data");
|
|
800
|
+
expect(text).toContain("cannot read this view");
|
|
801
|
+
expect(writes).toEqual([]);
|
|
802
|
+
});
|
|
803
|
+
|
|
804
|
+
test("the merge path still serves a block without applyParams", async () => {
|
|
805
|
+
// The same write conflicts in the merge and is served by a block that applies params.
|
|
806
|
+
const ours = data({ preset: { type: "file", file: "preset.json" } });
|
|
807
|
+
const { project, writes } = stubProject(storage(ours));
|
|
808
|
+
const text = errorText(
|
|
809
|
+
await writeBlockParams(
|
|
810
|
+
project,
|
|
811
|
+
author,
|
|
812
|
+
input({ params: { ...paramsOf(ours), preset: { type: "name", name: "other" } } }),
|
|
813
|
+
),
|
|
814
|
+
);
|
|
815
|
+
expect(text).toContain("preset.name");
|
|
816
|
+
expect(writes).toEqual([]);
|
|
817
|
+
|
|
818
|
+
const { model } = applyingModel();
|
|
819
|
+
const applied = stubProject(storage(ours), { model: async () => model });
|
|
820
|
+
const result = await writeBlockParams(
|
|
821
|
+
applied.project,
|
|
822
|
+
author,
|
|
823
|
+
input({ params: { ...paramsOf(ours), preset: { type: "name", name: "other" } } }),
|
|
824
|
+
);
|
|
825
|
+
expect(unchanged(result)).toBe(false);
|
|
826
|
+
expect(applied.writes[0].value).toEqual(data({ preset: { type: "name", name: "other" } }));
|
|
827
|
+
});
|
|
828
|
+
});
|