@niwa-labs/ui-kit 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 +29 -2
- package/package.json +31 -4
- package/src/agent/diff-view.tsx +62 -0
- package/src/agent/execution-track.tsx +201 -0
- package/src/agent/guard.ts +121 -0
- package/src/agent/guarded-action.tsx +272 -0
- package/src/agent/id-chip.tsx +102 -0
- package/src/agent/markdown.tsx +34 -0
- package/src/agent/payload.ts +42 -0
- package/src/agent/phase-plan.tsx +183 -0
- package/src/agent/plan-impact.ts +104 -0
- package/src/agent/state.tsx +75 -0
- package/src/agent/tool-call.tsx +106 -0
- package/src/agent/transcript.tsx +88 -0
- package/src/execution-track.css +327 -0
- package/src/index.ts +76 -0
- package/src/lib/cn.ts +19 -0
- package/src/page-layout.css +183 -0
- package/src/theme.css +136 -0
- package/src/ui/badge.tsx +32 -0
- package/src/ui/button.tsx +38 -0
- package/src/ui/collapsible.tsx +13 -0
- package/src/ui/data-value.tsx +211 -0
- package/src/ui/message.tsx +81 -0
- package/src/ui/page-layout.tsx +33 -0
- package/src/ui/schema-view.tsx +144 -0
- package/src/ui/spinner.tsx +16 -0
- package/src/ui/tabs.tsx +63 -0
- package/src/ui/tooltip.tsx +50 -0
- package/src/ui/trace.tsx +100 -0
- package/test/browser.test.ts +13 -0
- package/test/diff-view.test.tsx +50 -0
- package/test/guard.test.ts +113 -0
- package/test/guarded-action.test.tsx +229 -0
- package/test/helpers.test.ts +83 -0
- package/test/phase-plan.test.tsx +146 -0
- package/test/plan.test.ts +71 -0
- package/test/render.test.tsx +62 -0
- package/test/structured-values.test.tsx +31 -0
- package/test/tabs.test.tsx +90 -0
- package/tsconfig.json +16 -0
package/src/ui/trace.tsx
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Trace: the shape every agent step shares. A trace is a list of lines; a line has an icon, a
|
|
3
|
+
* title, an optional description and a status, and may open to show more. Tool calls and phase
|
|
4
|
+
* plans are built from these parts, so they look and behave the same.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { ChevronRightIcon } from "lucide-react";
|
|
8
|
+
import type { ComponentProps } from "react";
|
|
9
|
+
|
|
10
|
+
import { cn } from "../lib/cn.ts";
|
|
11
|
+
import { Collapsible, CollapsibleContent, CollapsibleTrigger } from "./collapsible.tsx";
|
|
12
|
+
|
|
13
|
+
export type TraceStatus = "running" | "error" | "pending";
|
|
14
|
+
|
|
15
|
+
export function Trace({ className, ...props }: ComponentProps<"div">) {
|
|
16
|
+
return <div data-slot="trace" className={cn("flex w-full min-w-0 flex-col gap-2", className)} {...props} />;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function TraceLine({ className, status, ...props }: ComponentProps<"span"> & { status?: TraceStatus | undefined }) {
|
|
20
|
+
return (
|
|
21
|
+
<span
|
|
22
|
+
data-slot="trace-line"
|
|
23
|
+
data-status={status}
|
|
24
|
+
className={cn(
|
|
25
|
+
"flex w-fit max-w-full min-w-0 items-center gap-2 text-sm text-muted-foreground transition-colors in-[a,button]:cursor-pointer in-[a:hover,button:hover]:text-foreground data-[status=error]:text-destructive data-[status=pending]:opacity-60 data-[status=running]:*:data-[slot=trace-title]:shimmer",
|
|
26
|
+
className,
|
|
27
|
+
)}
|
|
28
|
+
{...props}
|
|
29
|
+
/>
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function TraceIcon({ className, ...props }: ComponentProps<"span">) {
|
|
34
|
+
return (
|
|
35
|
+
<span
|
|
36
|
+
data-slot="trace-icon"
|
|
37
|
+
aria-hidden="true"
|
|
38
|
+
className={cn(
|
|
39
|
+
"flex size-4 shrink-0 items-center justify-center [&_svg]:pointer-events-none [&_svg:not([class*='size-'])]:size-4",
|
|
40
|
+
className,
|
|
41
|
+
)}
|
|
42
|
+
{...props}
|
|
43
|
+
/>
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function TraceTitle({ className, ...props }: ComponentProps<"span">) {
|
|
48
|
+
return <span data-slot="trace-title" className={cn("min-w-0 truncate motion-reduce:shimmer-none", className)} {...props} />;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function TraceDescription({ className, ...props }: ComponentProps<"span">) {
|
|
52
|
+
return (
|
|
53
|
+
<span
|
|
54
|
+
data-slot="trace-description"
|
|
55
|
+
className={cn("shrink-0 text-muted-foreground/70 tabular-nums", className)}
|
|
56
|
+
{...props}
|
|
57
|
+
/>
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function TraceCollapsible({ className, ...props }: ComponentProps<typeof Collapsible>) {
|
|
62
|
+
return <Collapsible data-slot="trace-collapsible" className={cn("flex w-full min-w-0 flex-col", className)} {...props} />;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function TraceCollapsibleTrigger({ className, children, ...props }: ComponentProps<typeof CollapsibleTrigger>) {
|
|
66
|
+
return (
|
|
67
|
+
<CollapsibleTrigger
|
|
68
|
+
data-slot="trace-collapsible-trigger"
|
|
69
|
+
className={cn(
|
|
70
|
+
"group/trace-trigger flex w-fit max-w-full min-w-0 cursor-pointer items-center gap-1 rounded-md text-left text-muted-foreground transition-colors outline-none hover:text-foreground focus-visible:ring-3 focus-visible:ring-ring/50",
|
|
71
|
+
className,
|
|
72
|
+
)}
|
|
73
|
+
{...props}
|
|
74
|
+
>
|
|
75
|
+
{children}
|
|
76
|
+
<TraceIcon className="transition-transform group-aria-expanded/trace-trigger:rotate-90">
|
|
77
|
+
<ChevronRightIcon />
|
|
78
|
+
</TraceIcon>
|
|
79
|
+
</CollapsibleTrigger>
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function TraceCollapsibleContent({ className, children, ...props }: ComponentProps<typeof CollapsibleContent>) {
|
|
84
|
+
return (
|
|
85
|
+
<CollapsibleContent
|
|
86
|
+
data-slot="trace-collapsible-content"
|
|
87
|
+
className="h-(--collapsible-panel-height) min-w-0 overflow-hidden transition-[height] duration-200 data-ending-style:h-0 data-starting-style:h-0"
|
|
88
|
+
{...props}
|
|
89
|
+
>
|
|
90
|
+
<div
|
|
91
|
+
className={cn(
|
|
92
|
+
"mt-2 mb-1 ml-[calc(--spacing(2)-1px)] min-w-0 border-l pr-1 pl-4 leading-relaxed wrap-break-word text-muted-foreground",
|
|
93
|
+
className,
|
|
94
|
+
)}
|
|
95
|
+
>
|
|
96
|
+
{children}
|
|
97
|
+
</div>
|
|
98
|
+
</CollapsibleContent>
|
|
99
|
+
);
|
|
100
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// The kit runs in the browser: its own code must not reach Node or Bun modules.
|
|
2
|
+
|
|
3
|
+
import { expect, test } from "bun:test";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
|
|
6
|
+
test("the entry bundles for the browser", async () => {
|
|
7
|
+
const entry = fileURLToPath(new URL("../src/index.ts", import.meta.url));
|
|
8
|
+
const result = await Bun.build({ entrypoints: [entry], target: "browser", packages: "external" });
|
|
9
|
+
expect(result.logs.filter((log) => log.level === "error")).toEqual([]);
|
|
10
|
+
expect(result.success).toBe(true);
|
|
11
|
+
const code = await result.outputs[0]!.text();
|
|
12
|
+
expect(code).not.toMatch(/from\s*["'](node:|bun:)/);
|
|
13
|
+
});
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// A patch holding several files renders each of them.
|
|
2
|
+
|
|
3
|
+
import { describe, expect, test } from "bun:test";
|
|
4
|
+
import { renderToString } from "react-dom/server";
|
|
5
|
+
|
|
6
|
+
import { DiffView } from "../src/index.ts";
|
|
7
|
+
|
|
8
|
+
const oneFile = `diff --git a/a.txt b/a.txt
|
|
9
|
+
index 111..222 100644
|
|
10
|
+
--- a/a.txt
|
|
11
|
+
+++ b/a.txt
|
|
12
|
+
@@ -1 +1 @@
|
|
13
|
+
-one
|
|
14
|
+
+two
|
|
15
|
+
`;
|
|
16
|
+
|
|
17
|
+
const otherFile = `diff --git a/b.txt b/b.txt
|
|
18
|
+
index 333..444 100644
|
|
19
|
+
--- a/b.txt
|
|
20
|
+
+++ b/b.txt
|
|
21
|
+
@@ -1 +1 @@
|
|
22
|
+
-x
|
|
23
|
+
+y
|
|
24
|
+
`;
|
|
25
|
+
|
|
26
|
+
function views(html: string): number {
|
|
27
|
+
return html.match(/<diffs-container/g)?.length ?? 0;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
describe("diff view", () => {
|
|
31
|
+
test("a patch of two files renders a diff for each", () => {
|
|
32
|
+
const html = renderToString(<DiffView patch={oneFile + otherFile} />);
|
|
33
|
+
expect(views(html)).toBe(2);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
test("a patch of one file renders it", () => {
|
|
37
|
+
expect(views(renderToString(<DiffView patch={oneFile} />))).toBe(1);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test("an empty patch renders nothing", () => {
|
|
41
|
+
expect(renderToString(<DiffView patch="" />)).toBe("");
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test("two versions of a file render one diff", () => {
|
|
45
|
+
const html = renderToString(
|
|
46
|
+
<DiffView before={{ name: "a.txt", contents: "one\n" }} after={{ name: "a.txt", contents: "two\n" }} />,
|
|
47
|
+
);
|
|
48
|
+
expect(views(html)).toBe(1);
|
|
49
|
+
});
|
|
50
|
+
});
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
|
|
3
|
+
import { firesOnUnmount, guardReducer, holdProgress, IDLE, nextDeadline, type GuardEvent, type GuardState } from "../src/index.ts";
|
|
4
|
+
|
|
5
|
+
const timing = { armMs: 4000, holdMs: 700, undoMs: 3000 };
|
|
6
|
+
|
|
7
|
+
function run(events: GuardEvent[], from: GuardState = IDLE): GuardState {
|
|
8
|
+
return events.reduce((state, event) => guardReducer(state, event, timing), from);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
describe("guard: two presses", () => {
|
|
12
|
+
test("the first press arms and the second confirms into the undo window", () => {
|
|
13
|
+
const armed = run([{ type: "press", now: 0 }]);
|
|
14
|
+
expect(armed).toEqual({ phase: "armed", until: 4000 });
|
|
15
|
+
expect(run([{ type: "press", now: 1000 }], armed)).toEqual({ phase: "pending", until: 4000 });
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
test("an arming expires and the next press arms again instead of confirming", () => {
|
|
19
|
+
const armed = run([{ type: "press", now: 0 }]);
|
|
20
|
+
expect(run([{ type: "tick", now: 3999 }], armed)).toEqual(armed);
|
|
21
|
+
expect(run([{ type: "tick", now: 4000 }], armed)).toEqual(IDLE);
|
|
22
|
+
// A press that lands after the deadline but before the tick does not confirm.
|
|
23
|
+
expect(run([{ type: "press", now: 5000 }], armed)).toEqual({ phase: "armed", until: 9000 });
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
test("the action fires only when the undo window closes", () => {
|
|
27
|
+
const pending = run([
|
|
28
|
+
{ type: "press", now: 0 },
|
|
29
|
+
{ type: "press", now: 100 },
|
|
30
|
+
]);
|
|
31
|
+
expect(run([{ type: "tick", now: 3099 }], pending).phase).toBe("pending");
|
|
32
|
+
const fired = run([{ type: "tick", now: 3100 }], pending);
|
|
33
|
+
expect(fired).toEqual({ phase: "fired" });
|
|
34
|
+
expect(run([{ type: "reset" }], fired)).toEqual(IDLE);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test("undo, Escape or blur before the window closes sends nothing", () => {
|
|
38
|
+
const armed = run([{ type: "press", now: 0 }]);
|
|
39
|
+
expect(run([{ type: "cancel" }], armed)).toEqual(IDLE);
|
|
40
|
+
const pending = run([{ type: "press", now: 10 }], armed);
|
|
41
|
+
expect(run([{ type: "cancel" }, { type: "tick", now: 99_999 }], pending)).toEqual(IDLE);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test("a fired action is not cancelled after the fact", () => {
|
|
45
|
+
expect(run([{ type: "cancel" }], { phase: "fired" })).toEqual({ phase: "fired" });
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
describe("guard: holding", () => {
|
|
50
|
+
test("a press held long enough confirms without a second press", () => {
|
|
51
|
+
const holding = run([{ type: "holdStart", now: 0 }]);
|
|
52
|
+
expect(holding).toEqual({ phase: "holding", since: 0, armedUntil: null });
|
|
53
|
+
expect(run([{ type: "holdEnd", now: 700 }], holding)).toEqual({ phase: "pending", until: 3700 });
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test("the hold completes on a tick while the pointer is still down; the release changes nothing", () => {
|
|
57
|
+
const pending = run([
|
|
58
|
+
{ type: "holdStart", now: 0 },
|
|
59
|
+
{ type: "tick", now: 700 },
|
|
60
|
+
]);
|
|
61
|
+
expect(pending).toEqual({ phase: "pending", until: 3700 });
|
|
62
|
+
expect(run([{ type: "holdEnd", now: 900 }], pending)).toEqual(pending);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
test("a short press is an ordinary press: it arms, and on an armed control it confirms", () => {
|
|
66
|
+
const armed = run([
|
|
67
|
+
{ type: "holdStart", now: 0 },
|
|
68
|
+
{ type: "holdEnd", now: 120 },
|
|
69
|
+
]);
|
|
70
|
+
expect(armed).toEqual({ phase: "armed", until: 4120 });
|
|
71
|
+
const confirmed = run(
|
|
72
|
+
[
|
|
73
|
+
{ type: "holdStart", now: 1000 },
|
|
74
|
+
{ type: "holdEnd", now: 1100 },
|
|
75
|
+
],
|
|
76
|
+
armed,
|
|
77
|
+
);
|
|
78
|
+
expect(confirmed).toEqual({ phase: "pending", until: 4100 });
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
test("leaving the control while holding goes back to where the hold started", () => {
|
|
82
|
+
expect(run([{ type: "holdStart", now: 0 }, { type: "holdCancel", now: 300 }])).toEqual(IDLE);
|
|
83
|
+
const armed = run([{ type: "press", now: 0 }]);
|
|
84
|
+
expect(run([{ type: "holdStart", now: 100 }, { type: "holdCancel", now: 300 }], armed)).toEqual(armed);
|
|
85
|
+
// The arming it came back to may have expired meanwhile.
|
|
86
|
+
expect(run([{ type: "holdStart", now: 100 }, { type: "holdCancel", now: 4500 }], armed)).toEqual(IDLE);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test("progress grows with the hold and is zero otherwise", () => {
|
|
90
|
+
const holding = run([{ type: "holdStart", now: 0 }]);
|
|
91
|
+
expect(holdProgress(holding, 350, timing)).toBeCloseTo(0.5);
|
|
92
|
+
expect(holdProgress(holding, 5000, timing)).toBe(1);
|
|
93
|
+
expect(holdProgress(IDLE, 350, timing)).toBe(0);
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
describe("guard: deadlines and unmounting", () => {
|
|
98
|
+
test("the next deadline is the one the current phase waits for", () => {
|
|
99
|
+
expect(nextDeadline(IDLE, timing)).toBeNull();
|
|
100
|
+
expect(nextDeadline({ phase: "armed", until: 10 }, timing)).toBe(10);
|
|
101
|
+
expect(nextDeadline({ phase: "holding", since: 5, armedUntil: null }, timing)).toBe(705);
|
|
102
|
+
expect(nextDeadline({ phase: "pending", until: 42 }, timing)).toBe(42);
|
|
103
|
+
expect(nextDeadline({ phase: "fired" }, timing)).toBeNull();
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
test("a confirmed action fires when its control goes away; an unconfirmed one does not", () => {
|
|
107
|
+
expect(firesOnUnmount({ phase: "pending", until: 1 })).toBe(true);
|
|
108
|
+
expect(firesOnUnmount({ phase: "fired" })).toBe(true);
|
|
109
|
+
expect(firesOnUnmount({ phase: "armed", until: 1 })).toBe(false);
|
|
110
|
+
expect(firesOnUnmount({ phase: "holding", since: 0, armedUntil: null })).toBe(false);
|
|
111
|
+
expect(firesOnUnmount(IDLE)).toBe(false);
|
|
112
|
+
});
|
|
113
|
+
});
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
// The guarded control in a document: real timers, short windows.
|
|
2
|
+
|
|
3
|
+
import { GlobalRegistrator } from "@happy-dom/global-registrator";
|
|
4
|
+
import { afterAll, beforeAll, describe, expect, test } from "bun:test";
|
|
5
|
+
import { Activity, act } from "react";
|
|
6
|
+
import { createRoot, type Root } from "react-dom/client";
|
|
7
|
+
|
|
8
|
+
import { GuardButton, GuardNotice, TooltipProvider, useGuardedAction } from "../src/index.ts";
|
|
9
|
+
|
|
10
|
+
declare global {
|
|
11
|
+
var IS_REACT_ACT_ENVIRONMENT: boolean | undefined;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
beforeAll(() => {
|
|
15
|
+
GlobalRegistrator.register();
|
|
16
|
+
globalThis.IS_REACT_ACT_ENVIRONMENT = true;
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
afterAll(async () => {
|
|
20
|
+
globalThis.IS_REACT_ACT_ENVIRONMENT = undefined;
|
|
21
|
+
await GlobalRegistrator.unregister();
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
const timing = { armMs: 1000, holdMs: 40, undoMs: 60 };
|
|
25
|
+
|
|
26
|
+
function Control({ onConfirm, disabled }: { onConfirm: () => void; disabled?: boolean }) {
|
|
27
|
+
const guard = useGuardedAction({ onConfirm, timing, disabled });
|
|
28
|
+
return (
|
|
29
|
+
<TooltipProvider>
|
|
30
|
+
<GuardButton guard={guard} icon={<span>R</span>} label="Recompute from here" />
|
|
31
|
+
<GuardNotice guard={guard} consequence="3 steps run again." pending="Recomputing" />
|
|
32
|
+
</TooltipProvider>
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async function mount(onConfirm: () => void): Promise<{ root: Root; host: HTMLElement }> {
|
|
37
|
+
const host = document.createElement("div");
|
|
38
|
+
document.body.append(host);
|
|
39
|
+
const root = createRoot(host);
|
|
40
|
+
await act(async () => root.render(<Control onConfirm={onConfirm} />));
|
|
41
|
+
return { root, host };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function button(host: HTMLElement): HTMLButtonElement {
|
|
45
|
+
return host.querySelector<HTMLButtonElement>('[data-slot="guard-button"]')!;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** A keyboard activation: a click event with no pointer behind it. */
|
|
49
|
+
async function keyPress(host: HTMLElement): Promise<void> {
|
|
50
|
+
await act(async () => {
|
|
51
|
+
button(host).dispatchEvent(new MouseEvent("click", { bubbles: true, detail: 0 }));
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async function wait(ms: number): Promise<void> {
|
|
56
|
+
await act(async () => {
|
|
57
|
+
await new Promise((resolve) => setTimeout(resolve, ms));
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
describe("guarded action", () => {
|
|
62
|
+
test("two presses explain, confirm, and fire once after the undo window", async () => {
|
|
63
|
+
let fired = 0;
|
|
64
|
+
const { root, host } = await mount(() => (fired += 1));
|
|
65
|
+
await keyPress(host);
|
|
66
|
+
expect(host.textContent).toContain("3 steps run again.");
|
|
67
|
+
await keyPress(host);
|
|
68
|
+
expect(host.textContent).toContain("Recomputing in 1s");
|
|
69
|
+
expect(fired).toBe(0);
|
|
70
|
+
await wait(120);
|
|
71
|
+
expect(fired).toBe(1);
|
|
72
|
+
expect(host.querySelector('[data-slot="guard-notice"]')).toBeNull();
|
|
73
|
+
await act(async () => root.unmount());
|
|
74
|
+
expect(fired).toBe(1);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test("undo during the window sends nothing", async () => {
|
|
78
|
+
let fired = 0;
|
|
79
|
+
const { root, host } = await mount(() => (fired += 1));
|
|
80
|
+
await keyPress(host);
|
|
81
|
+
await keyPress(host);
|
|
82
|
+
const undo = [...host.querySelectorAll("button")].find((b) => b.textContent?.includes("Undo"))!;
|
|
83
|
+
await act(async () => undo.click());
|
|
84
|
+
await wait(120);
|
|
85
|
+
expect(fired).toBe(0);
|
|
86
|
+
await act(async () => root.unmount());
|
|
87
|
+
expect(fired).toBe(0);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test("a control that goes away during the undo window fires what was confirmed", async () => {
|
|
91
|
+
let fired = 0;
|
|
92
|
+
const { root, host } = await mount(() => (fired += 1));
|
|
93
|
+
await keyPress(host);
|
|
94
|
+
await keyPress(host);
|
|
95
|
+
await act(async () => root.unmount());
|
|
96
|
+
expect(fired).toBe(1);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
test("Escape disarms", async () => {
|
|
100
|
+
let fired = 0;
|
|
101
|
+
const { root, host } = await mount(() => (fired += 1));
|
|
102
|
+
await keyPress(host);
|
|
103
|
+
await act(async () => {
|
|
104
|
+
button(host).dispatchEvent(new KeyboardEvent("keydown", { key: "Escape", bubbles: true }));
|
|
105
|
+
});
|
|
106
|
+
expect(host.querySelector('[data-slot="guard-notice"]')).toBeNull();
|
|
107
|
+
await act(async () => root.unmount());
|
|
108
|
+
expect(fired).toBe(0);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
test("the action fires for the target that was confirmed, not the one shown after", async () => {
|
|
112
|
+
const fired: string[] = [];
|
|
113
|
+
const host = document.createElement("div");
|
|
114
|
+
document.body.append(host);
|
|
115
|
+
const root = createRoot(host);
|
|
116
|
+
const show = (target: string) => <Control onConfirm={() => fired.push(target)} />;
|
|
117
|
+
await act(async () => root.render(show("A")));
|
|
118
|
+
await keyPress(host);
|
|
119
|
+
await keyPress(host);
|
|
120
|
+
await act(async () => root.render(show("B")));
|
|
121
|
+
await wait(120);
|
|
122
|
+
expect(fired).toEqual(["A"]);
|
|
123
|
+
await act(async () => root.unmount());
|
|
124
|
+
expect(fired).toEqual(["A"]);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
test("a confirmed action fires for its own target when the control goes away", async () => {
|
|
128
|
+
const fired: string[] = [];
|
|
129
|
+
const host = document.createElement("div");
|
|
130
|
+
document.body.append(host);
|
|
131
|
+
const root = createRoot(host);
|
|
132
|
+
const show = (target: string) => <Control onConfirm={() => fired.push(target)} />;
|
|
133
|
+
await act(async () => root.render(show("A")));
|
|
134
|
+
await keyPress(host);
|
|
135
|
+
await keyPress(host);
|
|
136
|
+
await act(async () => root.render(show("B")));
|
|
137
|
+
await act(async () => root.unmount());
|
|
138
|
+
expect(fired).toEqual(["A"]);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
test("a disabled control neither arms nor fires", async () => {
|
|
142
|
+
let fired = 0;
|
|
143
|
+
const host = document.createElement("div");
|
|
144
|
+
document.body.append(host);
|
|
145
|
+
const root = createRoot(host);
|
|
146
|
+
await act(async () => root.render(<Control onConfirm={() => (fired += 1)} disabled />));
|
|
147
|
+
expect(button(host).disabled).toBe(true);
|
|
148
|
+
await keyPress(host);
|
|
149
|
+
await keyPress(host);
|
|
150
|
+
await act(async () => {
|
|
151
|
+
button(host).dispatchEvent(new MouseEvent("pointerdown", { bubbles: true, button: 0 }));
|
|
152
|
+
});
|
|
153
|
+
await wait(120);
|
|
154
|
+
expect(host.querySelector('[data-slot="guard-notice"]')).toBeNull();
|
|
155
|
+
expect(fired).toBe(0);
|
|
156
|
+
await act(async () => root.unmount());
|
|
157
|
+
expect(fired).toBe(0);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
test("an armed control whose availability is revoked disarms", async () => {
|
|
161
|
+
let fired = 0;
|
|
162
|
+
const host = document.createElement("div");
|
|
163
|
+
document.body.append(host);
|
|
164
|
+
const root = createRoot(host);
|
|
165
|
+
await act(async () => root.render(<Control onConfirm={() => (fired += 1)} />));
|
|
166
|
+
await keyPress(host);
|
|
167
|
+
expect(host.textContent).toContain("3 steps run again.");
|
|
168
|
+
await act(async () => root.render(<Control onConfirm={() => (fired += 1)} disabled />));
|
|
169
|
+
expect(host.querySelector('[data-slot="guard-notice"]')).toBeNull();
|
|
170
|
+
await act(async () => root.render(<Control onConfirm={() => (fired += 1)} />));
|
|
171
|
+
await keyPress(host);
|
|
172
|
+
// Armed again from scratch: one more press only confirms, nothing was carried over.
|
|
173
|
+
expect(host.textContent).toContain("3 steps run again.");
|
|
174
|
+
expect(fired).toBe(0);
|
|
175
|
+
await act(async () => root.unmount());
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
test("a hold hidden before it completes is not completed when the control is shown again", async () => {
|
|
179
|
+
let fired = 0;
|
|
180
|
+
const host = document.createElement("div");
|
|
181
|
+
document.body.append(host);
|
|
182
|
+
const root = createRoot(host);
|
|
183
|
+
const show = (mode: "visible" | "hidden") => (
|
|
184
|
+
<Activity mode={mode}>
|
|
185
|
+
<Control onConfirm={() => (fired += 1)} />
|
|
186
|
+
</Activity>
|
|
187
|
+
);
|
|
188
|
+
await act(async () => root.render(show("visible")));
|
|
189
|
+
await act(async () => {
|
|
190
|
+
button(host).dispatchEvent(new MouseEvent("pointerdown", { bubbles: true, button: 0 }));
|
|
191
|
+
});
|
|
192
|
+
await act(async () => root.render(show("hidden")));
|
|
193
|
+
await wait(120);
|
|
194
|
+
await act(async () => root.render(show("visible")));
|
|
195
|
+
await wait(120);
|
|
196
|
+
expect(button(host).dataset.phase).toBe("idle");
|
|
197
|
+
expect(fired).toBe(0);
|
|
198
|
+
await act(async () => root.unmount());
|
|
199
|
+
expect(fired).toBe(0);
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
test("the hook refuses gestures while disabled, whatever element carries its props", async () => {
|
|
203
|
+
let fired = 0;
|
|
204
|
+
const seen = new Set<string>();
|
|
205
|
+
function Bare() {
|
|
206
|
+
const guard = useGuardedAction({ onConfirm: () => (fired += 1), timing, disabled: true });
|
|
207
|
+
const { disabled: _disabled, ...handlers } = guard.buttonProps;
|
|
208
|
+
seen.add(guard.state.phase);
|
|
209
|
+
return <span data-phase={guard.state.phase} {...handlers} />;
|
|
210
|
+
}
|
|
211
|
+
const host = document.createElement("div");
|
|
212
|
+
document.body.append(host);
|
|
213
|
+
const root = createRoot(host);
|
|
214
|
+
await act(async () => root.render(<Bare />));
|
|
215
|
+
const bare = host.querySelector<HTMLElement>("span")!;
|
|
216
|
+
await act(async () => {
|
|
217
|
+
bare.dispatchEvent(new MouseEvent("pointerdown", { bubbles: true, button: 0 }));
|
|
218
|
+
});
|
|
219
|
+
expect(bare.dataset.phase).toBe("idle");
|
|
220
|
+
await act(async () => {
|
|
221
|
+
bare.dispatchEvent(new MouseEvent("click", { bubbles: true, detail: 0 }));
|
|
222
|
+
});
|
|
223
|
+
expect(bare.dataset.phase).toBe("idle");
|
|
224
|
+
await act(async () => root.unmount());
|
|
225
|
+
expect(fired).toBe(0);
|
|
226
|
+
// Not even for a moment: the gesture is refused, not started and dropped again.
|
|
227
|
+
expect([...seen]).toEqual(["idle"]);
|
|
228
|
+
});
|
|
229
|
+
});
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
|
|
3
|
+
import { formatAgo, formatDuration, formatPayload, idPrefix, isPlainClick, isSettled, shortId } from "../src/index.ts";
|
|
4
|
+
|
|
5
|
+
describe("ids", () => {
|
|
6
|
+
test("the prefix is the lowercase word before the first dash", () => {
|
|
7
|
+
expect(idPrefix("run-1f3a9c")).toBe("run");
|
|
8
|
+
expect(idPrefix("s-0a1b2c")).toBe("s");
|
|
9
|
+
expect(idPrefix("owner/repo#12")).toBeUndefined();
|
|
10
|
+
expect(idPrefix("-abc")).toBeUndefined();
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
test("a short id keeps the prefix and the tail", () => {
|
|
14
|
+
expect(shortId("run-1f3a9c")).toBe("run-1f3a9c");
|
|
15
|
+
expect(shortId("task-0123456789abcdef")).toBe("task-…89abcdef");
|
|
16
|
+
expect(shortId("0123456789abcdefgh", 4)).toBe("…efgh");
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
test("only a plain left click is the app's to handle", () => {
|
|
20
|
+
const base = { button: 0, metaKey: false, ctrlKey: false, shiftKey: false, altKey: false, defaultPrevented: false };
|
|
21
|
+
expect(isPlainClick(base)).toBe(true);
|
|
22
|
+
expect(isPlainClick({ ...base, metaKey: true })).toBe(false);
|
|
23
|
+
expect(isPlainClick({ ...base, button: 1 })).toBe(false);
|
|
24
|
+
expect(isPlainClick({ ...base, defaultPrevented: true })).toBe(false);
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
describe("formatting", () => {
|
|
29
|
+
test("payloads: strings as they are, values as JSON, nothing for undefined", () => {
|
|
30
|
+
expect(formatPayload("ls -la")).toBe("ls -la");
|
|
31
|
+
expect(formatPayload({ a: 1 })).toBe('{\n "a": 1\n}');
|
|
32
|
+
expect(formatPayload(undefined)).toBe("");
|
|
33
|
+
const cyclic: Record<string, unknown> = {};
|
|
34
|
+
cyclic.self = cyclic;
|
|
35
|
+
expect(formatPayload(cyclic)).toBe("[object Object]");
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
test("durations", () => {
|
|
39
|
+
expect(formatDuration(850)).toBe("850ms");
|
|
40
|
+
expect(formatDuration(4200)).toBe("4.2s");
|
|
41
|
+
expect(formatDuration(42_000)).toBe("42s");
|
|
42
|
+
expect(formatDuration(185_000)).toBe("3m 05s");
|
|
43
|
+
expect(formatDuration(3_720_000)).toBe("1h 02m");
|
|
44
|
+
expect(formatDuration(-1)).toBe("");
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test("settled states", () => {
|
|
48
|
+
expect(isSettled("done")).toBe(true);
|
|
49
|
+
expect(isSettled("reused")).toBe(true);
|
|
50
|
+
expect(isSettled("running")).toBe(false);
|
|
51
|
+
expect(isSettled("waiting")).toBe(false);
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
describe("how long ago", () => {
|
|
56
|
+
const now = Date.parse("2026-11-21T12:00:00Z");
|
|
57
|
+
const at = (offsetMs: number): number => now - offsetMs;
|
|
58
|
+
|
|
59
|
+
test("a moment is told in the largest unit that fits", () => {
|
|
60
|
+
expect(formatAgo(at(10_000), now)).toBe("just now");
|
|
61
|
+
expect(formatAgo(at(5 * 60_000), now)).toBe("5m ago");
|
|
62
|
+
expect(formatAgo(at(3 * 3_600_000), now)).toBe("3h ago");
|
|
63
|
+
expect(formatAgo(at(2 * 86_400_000), now)).toBe("2d ago");
|
|
64
|
+
expect(formatAgo(at(60 * 86_400_000), now)).toBe("2026-09-22");
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test("an ISO time is read as the moment it names, and one that is none gives nothing", () => {
|
|
68
|
+
expect(formatAgo("2026-11-21T11:00:00Z", now)).toBe("1h ago");
|
|
69
|
+
expect(formatAgo("yesterday", now)).toBe("");
|
|
70
|
+
expect(formatAgo(Number.NaN, now)).toBe("");
|
|
71
|
+
expect(formatAgo(Number.POSITIVE_INFINITY, now)).toBe("");
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test("a number outside the range a date holds is no moment", () => {
|
|
75
|
+
expect(formatAgo(-1e20, now)).toBe("");
|
|
76
|
+
expect(formatAgo(1e20, now)).toBe("");
|
|
77
|
+
expect(formatAgo(8.64e15 + 1, now)).toBe("");
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test("a moment after now is not told as ago", () => {
|
|
81
|
+
expect(formatAgo(now + 120_000, now)).toBe("in the future");
|
|
82
|
+
});
|
|
83
|
+
});
|