@maxiaochao/pi-background-tasks 0.2.0 → 0.3.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/tests/viewer.mjs CHANGED
@@ -24,12 +24,13 @@ function fakeHerdr() {
24
24
  const [noun, verb, id] = args;
25
25
  if (noun === "tab" && verb === "create") {
26
26
  const tab = `w:t${++next}`, pane = `w:p${++next}`;
27
- tabs.set(tab, true);
27
+ tabs.set(tab, { label: args[args.indexOf("--label") + 1] });
28
28
  panes.set(pane, { tab, runs: [] });
29
29
  return { tab: { tab_id: tab }, root_pane: { pane_id: pane } };
30
30
  }
31
31
  if (noun === "tab") {
32
32
  if (!tabs.has(id)) throw missing("tab", id);
33
+ if (verb === "rename") tabs.get(id).label = args[3];
33
34
  if (verb === "close") {
34
35
  tabs.delete(id);
35
36
  for (const [pane, value] of panes) if (value.tab === id) panes.delete(pane);
@@ -37,6 +38,7 @@ function fakeHerdr() {
37
38
  return { tab: { tab_id: id, focused: state.focused } };
38
39
  }
39
40
  if (!panes.has(id)) throw missing("pane", id);
41
+ if (verb === "rename") panes.get(id).label = args[3];
40
42
  if (verb === "split") {
41
43
  const pane = `w:p${++next}`;
42
44
  panes.set(pane, { tab: panes.get(id).tab, runs: [] });
@@ -73,7 +75,7 @@ function harness(env = { HERDR_ENV: "1", HERDR_WORKSPACE_ID: "w" }) {
73
75
  register(pi, { operations: f.operations, clock: f.clock, logRoot: f.root, herdr: herdr.call, env, sleep: () => flush() });
74
76
  return {
75
77
  ...f, herdr, handlers, commands, notices, widgets, ctx,
76
- start: async (command) => (await tool.execute(command, { action: "start", command })).structuredContent.task,
78
+ start: async (command, description) => (await tool.execute(command, { action: "start", command, description })).structuredContent.task,
77
79
  begin: () => handlers.get("session_start")({}, ctx),
78
80
  async close() {
79
81
  const closing = handlers.get("session_shutdown")({}, ctx);
@@ -86,6 +88,69 @@ function harness(env = { HERDR_ENV: "1", HERDR_WORKSPACE_ID: "w" }) {
86
88
 
87
89
  async function settle() { for (let i = 0; i < 20; i++) await flush(); }
88
90
 
91
+ test("a single task names its tab, pane and visible header; watch and pane reuse update the names", async () => {
92
+ const h = harness();
93
+ try {
94
+ await h.begin();
95
+ const first = await h.start("make release", "Release build");
96
+ await settle();
97
+ const [tab] = h.herdr.tabs.keys();
98
+ const [left] = h.herdr.panes.keys();
99
+ assert.equal(h.herdr.tabs.get(tab).label, "BG · Release build");
100
+ assert.equal(h.herdr.panes.get(left).label, "01 · Release build");
101
+ assert.match(readFileSync(first.log_path.replace(/\.log$/, ".header"), "utf8"), /^01 · Release build\n\$ make release\n/);
102
+
103
+ await h.start("make test", "Unit tests");
104
+ await settle();
105
+ assert.equal(h.herdr.tabs.size, 1);
106
+ assert.equal(h.herdr.tabs.get(tab).label, "BG · Unit tests");
107
+ assert.equal([...h.herdr.panes.values()][1].label, "02 · Unit tests");
108
+ await h.commands.get("bg").handler(`watch ${first.id}`, h.ctx);
109
+ assert.equal(h.herdr.tabs.get(tab).label, "BG · Release build");
110
+ assert.equal(h.herdr.panes.get(left).runs.length, 1, "watch must not restart an existing viewer");
111
+
112
+ h.calls[0].resolve();
113
+ await settle();
114
+ const third = await h.start("make lint", "Lint");
115
+ await settle();
116
+ assert.equal(h.herdr.tabs.get(tab).label, "BG · Lint");
117
+ assert.equal(h.herdr.panes.get(left).label, "03 · Lint");
118
+ assert.match(readFileSync(third.log_path.replace(/\.log$/, ".header"), "utf8"), /^03 · Lint\n\$ make lint\n/);
119
+ assert.equal(h.herdr.panes.size, 2);
120
+ } finally { await h.close(); }
121
+ });
122
+
123
+ test("task numbers survive closing the left pane and reopening it beside another task", async () => {
124
+ const h = harness();
125
+ try {
126
+ await h.begin();
127
+ const first = await h.start("make build", "Build");
128
+ await h.start("make test", "Tests");
129
+ await settle();
130
+ const [left] = h.herdr.panes.keys();
131
+ h.herdr.closePane(left);
132
+ await h.commands.get("bg").handler(`watch ${first.id}`, h.ctx);
133
+ assert.deepEqual([...h.herdr.panes.values()].map((pane) => pane.label), ["02 · Tests", "01 · Build"]);
134
+ assert.match(readFileSync(first.log_path.replace(/\.log$/, ".header"), "utf8"), /^01 · Build\n/);
135
+ } finally { await h.close(); }
136
+ });
137
+
138
+ test("when the named task finishes, the tab names the remaining running task", async () => {
139
+ const h = harness();
140
+ try {
141
+ await h.begin();
142
+ await h.start("make build", "Build");
143
+ await h.start("make test", "Tests");
144
+ await settle();
145
+ const [tab] = h.herdr.tabs.keys();
146
+ assert.equal(h.herdr.tabs.get(tab).label, "BG · Tests");
147
+ h.calls[1].resolve();
148
+ await settle();
149
+ assert.equal(h.herdr.tabs.get(tab).label, "BG · Build");
150
+ assert.equal(h.herdr.tabs.size, 1);
151
+ } finally { await h.close(); }
152
+ });
153
+
89
154
  test("two tasks share one unfocused tab, one pane each; the pane shows the command, then the log", async () => {
90
155
  const h = harness();
91
156
  try {
@@ -101,7 +166,7 @@ test("two tasks share one unfocused tab, one pane each; the pane shows the comma
101
166
  const runs = [...h.herdr.panes.values()].map((pane) => pane.runs.at(-1));
102
167
  assert.ok(runs[0].includes(first.log_path) && runs[1].includes(second.log_path));
103
168
  assert.doesNotMatch(runs.join("\n"), /make test|printf/, "the command text is never passed as shell syntax");
104
- assert.match(readFileSync(first.log_path.replace(/\.log$/, ".header"), "utf8"), /^\$ printf 'a\\n'; sleep 1\n# bg-.* · cwd .* · timeout 30m\n/);
169
+ assert.match(readFileSync(first.log_path.replace(/\.log$/, ".header"), "utf8"), /^01 · printf 'a\\n'; sleep 1\n\$ printf 'a\\n'; sleep 1\n# bg-.* · cwd .* · timeout 30m\n/);
105
170
  } finally { await h.close(); }
106
171
  });
107
172
 
@@ -156,6 +221,8 @@ test("closing a pane keeps the task running; /bg watch reopens and focuses it",
156
221
  assert.equal(h.herdr.tabs.size, 1);
157
222
  assert.equal(h.herdr.panes.size, 1);
158
223
  assert.ok([...h.herdr.panes.values()][0].runs.at(-1).includes(task.log_path));
224
+ assert.equal([...h.herdr.tabs.values()][0].label, "BG · one");
225
+ assert.equal([...h.herdr.panes.values()][0].label, "01 · one");
159
226
  assert.ok(h.herdr.calls.some((args) => args[0] === "tab" && args[1] === "focus"));
160
227
  assert.deepEqual(h.notices, []);
161
228
  } finally { await h.close(); }
@@ -190,6 +257,27 @@ test("clicking a widget task row opens its view and focuses the tab; other click
190
257
  } finally { await h.close(); }
191
258
  });
192
259
 
260
+ test("completion after the user closes the tab does not consume the warning for real Herdr failures", async () => {
261
+ const h = harness();
262
+ try {
263
+ await h.begin();
264
+ await h.start("one");
265
+ await h.start("two");
266
+ await settle();
267
+ const [tab] = h.herdr.tabs.keys();
268
+ await h.herdr.call(["tab", "close", tab]);
269
+ h.calls[0].resolve();
270
+ await settle();
271
+ assert.deepEqual(h.notices, [], "closing a view is not a Herdr failure");
272
+ assert.equal(h.calls[1].signal.aborted, false);
273
+ h.herdr.state.fail = () => true;
274
+ await h.start("three");
275
+ await settle();
276
+ assert.equal(h.notices.length, 1, "a later real failure still warns");
277
+ assert.equal(h.notices[0].level, "warning");
278
+ } finally { await h.close(); }
279
+ });
280
+
193
281
  test("Herdr failures warn once and never affect tasks", async () => {
194
282
  const h = harness();
195
283
  try {