@niwa-labs/tasks 0.0.0-stage → 0.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/LICENSE +21 -0
- package/README.md +4 -2
- package/package.json +42 -4
- package/src/actions.ts +109 -0
- package/src/cockpit/actions.ts +52 -0
- package/src/cockpit/index.tsx +32 -0
- package/src/cockpit/pages.tsx +103 -0
- package/src/executor.ts +214 -0
- package/src/index.ts +516 -0
- package/src/ledger.ts +88 -0
- package/src/owner.ts +178 -0
- package/src/records-entry.ts +38 -0
- package/src/records.ts +118 -0
- package/src/strategy.ts +60 -0
- package/test/asker.test.ts +67 -0
- package/test/cockpit-actions.test.ts +19 -0
- package/test/executor.test.ts +520 -0
- package/test/ledger.test.ts +172 -0
- package/test/notifications.test.ts +170 -0
- package/test/queries.test.ts +19 -0
- package/test/steps.test.ts +147 -0
- package/test/tasks.test.ts +536 -0
- package/tsconfig.json +15 -0
package/src/owner.ts
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The rules of a task. The state is folded from the journal, never kept in memory, and a decision
|
|
3
|
+
* says whether a command is allowed before anything is written.
|
|
4
|
+
*
|
|
5
|
+
* - a task finishes once: the first end stands, and a second end is refused;
|
|
6
|
+
* - only the responsible party reports and finishes;
|
|
7
|
+
* - a report or a finish of a task that does not exist is refused;
|
|
8
|
+
* - a result that does not fit the schema the task declared is refused, and the task stays open;
|
|
9
|
+
* - so is an end too large to be kept as one record.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { z } from "zod";
|
|
13
|
+
|
|
14
|
+
import { CatchableError, RECORD_DATA_LIMIT } from "@niwa-labs/core";
|
|
15
|
+
import type { Address } from "@niwa-labs/core";
|
|
16
|
+
import type { ResultSchema } from "@niwa-labs/messages/records";
|
|
17
|
+
|
|
18
|
+
import { TaskCreated, TaskDone } from "./records.ts";
|
|
19
|
+
import type { TaskContract, TaskId, TaskOutcome } from "./records.ts";
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The commands that can be made to a task.
|
|
23
|
+
*
|
|
24
|
+
* A finish with no outcome asks only whether the party may end the task; one with an outcome is
|
|
25
|
+
* also held to the result schema the task declared.
|
|
26
|
+
*/
|
|
27
|
+
export type Command =
|
|
28
|
+
| { readonly kind: "create"; readonly contract: TaskContract }
|
|
29
|
+
| { readonly kind: "report"; readonly task: TaskId; readonly by: Address }
|
|
30
|
+
| {
|
|
31
|
+
readonly kind: "finish";
|
|
32
|
+
readonly task: TaskId;
|
|
33
|
+
readonly by: Address;
|
|
34
|
+
readonly outcome?: TaskOutcome;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/** What a task is now. */
|
|
38
|
+
export type TaskState =
|
|
39
|
+
| { readonly status: "open"; readonly task: TaskId; readonly contract: TaskContract }
|
|
40
|
+
| {
|
|
41
|
+
readonly status: "done";
|
|
42
|
+
readonly task: TaskId;
|
|
43
|
+
readonly contract: TaskContract;
|
|
44
|
+
readonly outcome: TaskOutcome;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/** Why a command was refused. */
|
|
48
|
+
export type TaskRefusalCode =
|
|
49
|
+
| "unbound"
|
|
50
|
+
| "not-asker"
|
|
51
|
+
| "exists"
|
|
52
|
+
| "unknown"
|
|
53
|
+
| "done"
|
|
54
|
+
| "not-responsible"
|
|
55
|
+
| "result-schema"
|
|
56
|
+
| "result-invalid"
|
|
57
|
+
| "result-too-large"
|
|
58
|
+
| "no-task"
|
|
59
|
+
| "not-an-executor"
|
|
60
|
+
| "not-the-assignment";
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* A refusal: a command that breaks a rule. Nothing was written, so it is the caller's to catch
|
|
64
|
+
* (a result that does not fit is corrected and sent again), not a failed step.
|
|
65
|
+
*/
|
|
66
|
+
export class TaskRefusal extends CatchableError {
|
|
67
|
+
override name = "TaskRefusal";
|
|
68
|
+
/** The code of the refusal, so a caller can tell one from another. */
|
|
69
|
+
override readonly code: TaskRefusalCode;
|
|
70
|
+
|
|
71
|
+
constructor(code: TaskRefusalCode, message: string) {
|
|
72
|
+
super(message);
|
|
73
|
+
this.code = code;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* The state of every task, folded from the facts about them.
|
|
79
|
+
*
|
|
80
|
+
* The first creation and the first end of a task stand; a later one of the same task is passed
|
|
81
|
+
* over, as is a record that is not a fact about a task.
|
|
82
|
+
*/
|
|
83
|
+
export function fold(facts: readonly unknown[]): Map<string, TaskState> {
|
|
84
|
+
const states = new Map<string, TaskState>();
|
|
85
|
+
for (const fact of facts) {
|
|
86
|
+
const created = TaskCreated.schema.safeParse(fact);
|
|
87
|
+
if (created.success) {
|
|
88
|
+
if (!states.has(created.data.id)) {
|
|
89
|
+
states.set(created.data.id, { status: "open", task: created.data.id, contract: created.data });
|
|
90
|
+
}
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
const done = TaskDone.schema.safeParse(fact);
|
|
94
|
+
if (done.success) {
|
|
95
|
+
const state = states.get(done.data.task);
|
|
96
|
+
if (state === undefined || state.status === "done") continue;
|
|
97
|
+
states.set(done.data.task, { ...state, status: "done", outcome: done.data.outcome });
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return states;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** The state of one task, from the facts about it. */
|
|
104
|
+
export function stateOf(facts: readonly unknown[], task: string): TaskState | undefined {
|
|
105
|
+
return fold(facts).get(task);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** A declared result schema, read as a schema that checks. */
|
|
109
|
+
function readResultSchema(schema: ResultSchema): z.ZodType<unknown> {
|
|
110
|
+
return z.fromJSONSchema(schema as Parameters<typeof z.fromJSONSchema>[0]);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** A result schema is refused unless it describes an object and can be read. */
|
|
114
|
+
function checkResultSchema(schema: ResultSchema): void {
|
|
115
|
+
if (schema.type !== "object") {
|
|
116
|
+
throw new TaskRefusal("result-schema", 'the result schema must describe an object: its "type" must be "object"');
|
|
117
|
+
}
|
|
118
|
+
try {
|
|
119
|
+
readResultSchema(schema);
|
|
120
|
+
} catch (error) {
|
|
121
|
+
throw new TaskRefusal("result-schema", `the result schema cannot be read: ${error instanceof Error ? error.message : String(error)}`);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** Refuse a result that does not fit the schema the task declared; a task that declared none takes any. */
|
|
126
|
+
function checkResult(contract: TaskContract, outcome: TaskOutcome): void {
|
|
127
|
+
if (outcome.status !== "result" || contract.result === undefined) return;
|
|
128
|
+
const parsed = readResultSchema(contract.result).safeParse(outcome.result);
|
|
129
|
+
if (parsed.success) return;
|
|
130
|
+
const issues = parsed.error.issues.map((issue) => `${issue.path.join(".") || "(result)"}: ${issue.message}`).join("; ");
|
|
131
|
+
throw new TaskRefusal("result-invalid", `the result does not fit the schema the task "${contract.id}" declared: ${issues}`);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/** Can the end of a task be kept as one record? A record holds so much, and an end past it is not written. */
|
|
135
|
+
export function fitsRecord(task: TaskId, outcome: TaskOutcome): boolean {
|
|
136
|
+
return Buffer.byteLength(JSON.stringify({ task, outcome })) <= RECORD_DATA_LIMIT;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** Refuse a command that breaks a rule. Nothing is written here: the caller writes the fact. */
|
|
140
|
+
export function decide(state: TaskState | undefined, command: Command): void {
|
|
141
|
+
if (command.kind === "create") {
|
|
142
|
+
if (state !== undefined) {
|
|
143
|
+
throw new TaskRefusal("exists", `the task "${command.contract.id}" already exists`);
|
|
144
|
+
}
|
|
145
|
+
if (command.contract.result !== undefined) checkResultSchema(command.contract.result);
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
if (state === undefined) {
|
|
149
|
+
throw new TaskRefusal("unknown", `the task "${command.task}" does not exist`);
|
|
150
|
+
}
|
|
151
|
+
if (state.status === "done") {
|
|
152
|
+
throw new TaskRefusal("done", `the task "${command.task}" is already done`);
|
|
153
|
+
}
|
|
154
|
+
if (!sameAddress(command.by, state.contract.responsible)) {
|
|
155
|
+
throw new TaskRefusal(
|
|
156
|
+
"not-responsible",
|
|
157
|
+
`only the responsible party of the task "${command.task}" may ${command.kind} it`,
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
if (command.kind === "finish" && command.outcome !== undefined) {
|
|
161
|
+
checkResult(state.contract, command.outcome);
|
|
162
|
+
if (!fitsRecord(command.task, command.outcome)) {
|
|
163
|
+
throw new TaskRefusal(
|
|
164
|
+
"result-too-large",
|
|
165
|
+
`the end of the task "${command.task}" is more than ${String(RECORD_DATA_LIMIT)} bytes, which is more than a record holds`,
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** Are two addresses the same one? They are when they say the same thing, kind included. */
|
|
172
|
+
export function sameAddress(one: Address, other: Address): boolean {
|
|
173
|
+
const left = Object.entries(one);
|
|
174
|
+
return (
|
|
175
|
+
left.length === Object.keys(other).length &&
|
|
176
|
+
left.every(([key, value]) => (other as Record<string, unknown>)[key] === value)
|
|
177
|
+
);
|
|
178
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The records entry of the tasks module: what another plugin may import from it. The records, the
|
|
3
|
+
* typed references of the actions, and the pure functions that fold the state of a task from the
|
|
4
|
+
* journal; no factory and nothing that runs. The actions import the records, so this file sits
|
|
5
|
+
* above both and neither imports it.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export {
|
|
9
|
+
TASK_TYPES,
|
|
10
|
+
TaskContract,
|
|
11
|
+
TaskCreated,
|
|
12
|
+
TaskDone,
|
|
13
|
+
TaskId,
|
|
14
|
+
TaskKind,
|
|
15
|
+
TaskOutcome,
|
|
16
|
+
TaskUpdated,
|
|
17
|
+
taskDone,
|
|
18
|
+
taskUpdates,
|
|
19
|
+
} from "./records.ts";
|
|
20
|
+
export {
|
|
21
|
+
CreateInput,
|
|
22
|
+
FailInput,
|
|
23
|
+
FinishInput,
|
|
24
|
+
OutcomeOutput,
|
|
25
|
+
ReportInput,
|
|
26
|
+
TaskEntity,
|
|
27
|
+
TaskStatus,
|
|
28
|
+
taskAssigned,
|
|
29
|
+
taskCreate,
|
|
30
|
+
taskFail,
|
|
31
|
+
taskFinish,
|
|
32
|
+
taskGet,
|
|
33
|
+
taskList,
|
|
34
|
+
taskReport,
|
|
35
|
+
taskResult,
|
|
36
|
+
} from "./actions.ts";
|
|
37
|
+
export { TaskRefusal, fold, sameAddress, stateOf } from "./owner.ts";
|
|
38
|
+
export type { TaskState } from "./owner.ts";
|
package/src/records.ts
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The records of the tasks module: a task was created, a task was updated, a task is done.
|
|
3
|
+
*
|
|
4
|
+
* A task is a contract and nothing else: what to do, who asked,
|
|
5
|
+
* who is responsible, where to report, what kind of work it is, and how it ended. The records
|
|
6
|
+
* hold that contract and nothing more — no session, no model, no way of working. Those are
|
|
7
|
+
* chosen by intercepts and strategies, and are never encoded in advance.
|
|
8
|
+
*
|
|
9
|
+
* The kind of work is an open label: a plain string, never a closed list in the core.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { Address, EntityDescription, JsonValueSchema, idSchema, principalSchema, queryOf, record } from "@niwa-labs/core";
|
|
13
|
+
import type { Id, TypeQuery } from "@niwa-labs/core";
|
|
14
|
+
import { z } from "zod";
|
|
15
|
+
|
|
16
|
+
import { ResultSchema } from "@niwa-labs/messages/records";
|
|
17
|
+
|
|
18
|
+
/** The id of a task: `task-3fa9c2`. Niwa issues it when the task is made; the creator never chooses it. */
|
|
19
|
+
export const TaskId = idSchema("task");
|
|
20
|
+
export type TaskId = Id<"task">;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The kind of work a task is.
|
|
24
|
+
*
|
|
25
|
+
* An open label on purpose: the kinds of work are not a closed list in the core.
|
|
26
|
+
*/
|
|
27
|
+
export const TaskKind = z.string().min(1);
|
|
28
|
+
|
|
29
|
+
/** How a task ended: a result or a failure. */
|
|
30
|
+
export const TaskOutcome = z.discriminatedUnion("status", [
|
|
31
|
+
z.object({ status: z.literal("result"), result: JsonValueSchema }),
|
|
32
|
+
z.object({ status: z.literal("failure"), reason: z.string().min(1) }),
|
|
33
|
+
]);
|
|
34
|
+
|
|
35
|
+
/** How a task ended. */
|
|
36
|
+
export type TaskOutcome = z.infer<typeof TaskOutcome>;
|
|
37
|
+
|
|
38
|
+
/** What a task holds when it is created. */
|
|
39
|
+
export const TaskContract = z.object({
|
|
40
|
+
/** The id of the task. */
|
|
41
|
+
id: TaskId,
|
|
42
|
+
/** The kind of work this is: an open label. */
|
|
43
|
+
kind: TaskKind,
|
|
44
|
+
/** A few words a list shows, from whoever made the task. */
|
|
45
|
+
description: EntityDescription,
|
|
46
|
+
/** What to do. */
|
|
47
|
+
text: z.string().min(1),
|
|
48
|
+
/** Who asked for the work. */
|
|
49
|
+
askedBy: Address,
|
|
50
|
+
/** Who is responsible for it: one address. */
|
|
51
|
+
responsible: Address,
|
|
52
|
+
/** Where updates and the result go. */
|
|
53
|
+
reportTo: Address,
|
|
54
|
+
/**
|
|
55
|
+
* The shape the result must have: a JSON Schema of an object, declared by whoever asked for the work.
|
|
56
|
+
* `task.finish` checks the result against it, and whoever does the work is shown it.
|
|
57
|
+
*/
|
|
58
|
+
result: ResultSchema.optional(),
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
/** One task, as it was created. */
|
|
62
|
+
export type TaskContract = z.infer<typeof TaskContract>;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* A task was created.
|
|
66
|
+
*
|
|
67
|
+
* The record holds the whole contract, so the task can be read without knowing anything else.
|
|
68
|
+
*/
|
|
69
|
+
export const TaskCreated = record("task.created", TaskContract.extend({ initiator: principalSchema.optional() }));
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* A task was updated.
|
|
73
|
+
*
|
|
74
|
+
* An update is what the responsible party says while the work goes on: a note, a progress
|
|
75
|
+
* report. It says nothing about the end of the task.
|
|
76
|
+
*/
|
|
77
|
+
export const TaskUpdated = record(
|
|
78
|
+
"task.updated",
|
|
79
|
+
z.object({
|
|
80
|
+
/** The task that was updated. */
|
|
81
|
+
task: TaskId,
|
|
82
|
+
/** What was reported. */
|
|
83
|
+
text: z.string().min(1),
|
|
84
|
+
/** Anything the reporter wants to carry along. */
|
|
85
|
+
data: JsonValueSchema.optional(),
|
|
86
|
+
}),
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* A task is done.
|
|
91
|
+
*
|
|
92
|
+
* A task ends once: a second end of the same task is refused, and the first one stands.
|
|
93
|
+
*/
|
|
94
|
+
export const TaskDone = record(
|
|
95
|
+
"task.done",
|
|
96
|
+
z.object({
|
|
97
|
+
/** The task that is done. */
|
|
98
|
+
task: TaskId,
|
|
99
|
+
/** How it ended. */
|
|
100
|
+
outcome: TaskOutcome,
|
|
101
|
+
}),
|
|
102
|
+
);
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* The end of one task: what a script waits for (`wait(taskDone(task.id), { timeout })`), a reader
|
|
106
|
+
* reads and a handler subscribes to. A text that is not the id of a task is refused here.
|
|
107
|
+
*/
|
|
108
|
+
export function taskDone(task: string): TypeQuery<z.output<typeof TaskDone.schema>> {
|
|
109
|
+
return queryOf(TaskDone, { task: TaskId.parse(task) });
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** The updates of one task, in the order they were written. */
|
|
113
|
+
export function taskUpdates(task: string): TypeQuery<z.output<typeof TaskUpdated.schema>> {
|
|
114
|
+
return queryOf(TaskUpdated, { task: TaskId.parse(task) });
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** Every record type of the tasks module. */
|
|
118
|
+
export const TASK_TYPES = [TaskCreated, TaskUpdated, TaskDone] as const;
|
package/src/strategy.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The contract a strategy implements as the responsible party of a task.
|
|
3
|
+
*
|
|
4
|
+
* A task is a contract and nothing else. How the work is brought
|
|
5
|
+
* to a result is not part of the task: it is the business of the party that is responsible for it.
|
|
6
|
+
*
|
|
7
|
+
* The responsible party is always one address. It may be an agent, which then simply does the
|
|
8
|
+
* work. It may be a strategy: a plugin that accepts the task, starts sessions and subtasks as it
|
|
9
|
+
* sees fit, and answers for the result. A new way of working is a new strategy; the base does not
|
|
10
|
+
* change.
|
|
11
|
+
*
|
|
12
|
+
* So a strategy is two things and no more:
|
|
13
|
+
*
|
|
14
|
+
* - it accepts a task, or it does not;
|
|
15
|
+
* - it answers for the result of a task it accepted.
|
|
16
|
+
*
|
|
17
|
+
* Nothing else is asked of it. The base does not tell a strategy how to work, does not count its
|
|
18
|
+
* attempts, and does not supervise it.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import type { JsonValue, Address } from "@niwa-labs/core";
|
|
22
|
+
|
|
23
|
+
import type { TaskContract } from "./records.ts";
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* A strategy: the way a task is brought to a result.
|
|
27
|
+
*
|
|
28
|
+
* The contract is small on purpose: a strategy that accepts a task is asked for the result, and
|
|
29
|
+
* nothing else.
|
|
30
|
+
*/
|
|
31
|
+
export interface Strategy {
|
|
32
|
+
/**
|
|
33
|
+
* The address this strategy speaks as.
|
|
34
|
+
*
|
|
35
|
+
* It is the address of the task's responsible party: a task assigned to a strategy names it
|
|
36
|
+
* here.
|
|
37
|
+
*/
|
|
38
|
+
readonly address: Address;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Accept a task.
|
|
42
|
+
*
|
|
43
|
+
* Return true when the strategy takes the task, and false when it does not: a strategy that
|
|
44
|
+
* does not accept a task leaves it to somebody else. The base does not ask again.
|
|
45
|
+
*/
|
|
46
|
+
accept(task: TaskContract): boolean | Promise<boolean>;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Answer for the result of a task.
|
|
50
|
+
*
|
|
51
|
+
* The strategy does the work however it likes, and says how the task ended. The base writes
|
|
52
|
+
* what it says: the task is finished, once.
|
|
53
|
+
*/
|
|
54
|
+
answer(task: TaskContract): Promise<TaskAnswer>;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** How a task ended, as a strategy says it. */
|
|
58
|
+
export type TaskAnswer =
|
|
59
|
+
| { readonly status: "result"; readonly result: JsonValue }
|
|
60
|
+
| { readonly status: "failure"; readonly reason: string };
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// Who asks for a task: the address the caller speaks as, never one the input names.
|
|
2
|
+
//
|
|
3
|
+
// An Assignment goes out from the asker, and whoever reads it takes the asker for the author of the
|
|
4
|
+
// words. So a client that is bound to an address asks as that address, and a payload that names
|
|
5
|
+
// another is refused; the host and the handlers of plugins, which stand for nobody, may ask in
|
|
6
|
+
// another's name.
|
|
7
|
+
|
|
8
|
+
import { expect, test } from "bun:test";
|
|
9
|
+
|
|
10
|
+
import type { Address, Caller } from "@niwa-labs/core";
|
|
11
|
+
import { MessageSent } from "@niwa-labs/messages/records";
|
|
12
|
+
import type { CallerBinding } from "@niwa-labs/plugin";
|
|
13
|
+
import { connectAsOwner, createNiwa, messages, tempHome } from "@niwa-labs/testing";
|
|
14
|
+
|
|
15
|
+
import { taskCreate, tasks } from "../src/index.ts";
|
|
16
|
+
|
|
17
|
+
/** The client called "asker" speaks as itself; no other client is known to the binding. */
|
|
18
|
+
const bind: CallerBinding = (caller: Caller) =>
|
|
19
|
+
caller.kind === "client" && String(caller.id).includes("asker") ? { kind: "client", id: "asker" } : undefined;
|
|
20
|
+
|
|
21
|
+
const OWNER: Address = { kind: "owner" };
|
|
22
|
+
|
|
23
|
+
const contract = (askedBy: Address) => ({
|
|
24
|
+
kind: "change",
|
|
25
|
+
description: "Move argument parsing",
|
|
26
|
+
text: "Move argument parsing into its own module.",
|
|
27
|
+
askedBy,
|
|
28
|
+
responsible: { kind: "agent", id: "rin" } as Address,
|
|
29
|
+
reportTo: askedBy,
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test("a client asks as the address it is bound to, and cannot ask in the name of the owner", async () => {
|
|
33
|
+
const home = await tempHome();
|
|
34
|
+
const niwa = createNiwa({ home: home.path });
|
|
35
|
+
niwa.use(messages({ bind }));
|
|
36
|
+
niwa.use(tasks({ messages: { bind } }));
|
|
37
|
+
await niwa.serve();
|
|
38
|
+
|
|
39
|
+
try {
|
|
40
|
+
const asker = await connectAsOwner(home.path, "asker");
|
|
41
|
+
const nobody = await connectAsOwner(home.path, "nobody");
|
|
42
|
+
try {
|
|
43
|
+
// A payload that names the owner does not make a client the owner.
|
|
44
|
+
await expect(asker.call(taskCreate, contract(OWNER))).rejects.toThrow("not by one the input names");
|
|
45
|
+
// A client the binding does not know does not choose an address at all.
|
|
46
|
+
await expect(nobody.call(taskCreate, contract({ kind: "client", id: "nobody" }))).rejects.toThrow("not bound");
|
|
47
|
+
expect(await niwa.records.read({ types: [MessageSent.name] })).toHaveLength(0);
|
|
48
|
+
|
|
49
|
+
// As itself, a client asks, and the Assignment comes from it.
|
|
50
|
+
const own: Address = { kind: "client", id: "asker" };
|
|
51
|
+
const { id } = await asker.call(taskCreate, contract(own));
|
|
52
|
+
await Bun.sleep(200);
|
|
53
|
+
const sent = await niwa.records.read({ types: [MessageSent.name] });
|
|
54
|
+
expect(sent.map((stored) => (stored.data as { from: Address; task: string }).from)).toEqual([own]);
|
|
55
|
+
expect(sent.map((stored) => (stored.data as { task: string }).task)).toEqual([id]);
|
|
56
|
+
|
|
57
|
+
// The host stands for nobody: it asks in the name of whom it likes.
|
|
58
|
+
await niwa.call(taskCreate, contract(OWNER));
|
|
59
|
+
} finally {
|
|
60
|
+
await asker.close().catch(() => {});
|
|
61
|
+
await nobody.close().catch(() => {});
|
|
62
|
+
}
|
|
63
|
+
} finally {
|
|
64
|
+
await niwa.close().catch(() => {});
|
|
65
|
+
await home.cleanup();
|
|
66
|
+
}
|
|
67
|
+
}, 30_000);
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// The copy of the task actions the cockpit module keeps reads what the host answers and sends what it takes.
|
|
2
|
+
|
|
3
|
+
import { expect, test } from "bun:test";
|
|
4
|
+
import { unreadable, unsendable } from "@niwa-labs/testing";
|
|
5
|
+
|
|
6
|
+
import { taskGet, taskList } from "../src/index.ts";
|
|
7
|
+
import { getAction, listAction } from "../src/cockpit/actions.ts";
|
|
8
|
+
|
|
9
|
+
test("the task list is read from what the host answers", () => {
|
|
10
|
+
expect(listAction.name).toBe(String(taskList.name));
|
|
11
|
+
expect(unsendable(listAction.input, taskList.input)).toEqual([]);
|
|
12
|
+
expect(unreadable(listAction.output, taskList.output)).toEqual([]);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
test("one task is read from what the host answers", () => {
|
|
16
|
+
expect(getAction.name).toBe(String(taskGet.name));
|
|
17
|
+
expect(unsendable(getAction.input, taskGet.input)).toEqual([]);
|
|
18
|
+
expect(unreadable(getAction.output, taskGet.output)).toEqual([]);
|
|
19
|
+
});
|