@alepha/ui 0.25.0 → 0.25.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +4 -5
- package/src/components/account/__tests__/account-router.spec.ts +201 -0
- package/src/components/account/account-connections.tsx +103 -0
- package/src/components/account/account-delete-dialog.tsx +184 -0
- package/src/components/account/account-header.tsx +57 -0
- package/src/components/account/account-keys.tsx +221 -0
- package/src/components/account/account-layout.tsx +63 -0
- package/src/components/account/account-password-dialog.tsx +152 -0
- package/src/components/account/account-profile.tsx +229 -0
- package/src/components/account/account-router-options.tsx +74 -0
- package/src/components/account/account-router-page.tsx +69 -0
- package/src/components/account/account-router.tsx +214 -0
- package/src/components/account/account-security.tsx +150 -0
- package/src/components/account/account-sessions.tsx +140 -0
- package/src/components/admin/__tests__/admin-keys-token-dialog.browser.spec.tsx +62 -0
- package/src/components/admin/__tests__/admin-layout.browser.spec.tsx +82 -0
- package/src/components/admin/__tests__/admin-router-page.spec.ts +54 -0
- package/src/components/admin/__tests__/admin-router.spec.ts +208 -0
- package/src/components/admin/__tests__/admin-user-cell.browser.spec.tsx +99 -0
- package/src/components/admin/__tests__/admin-user-detail.spec.ts +29 -0
- package/src/components/admin/admin-analytics-query-panel.tsx +149 -0
- package/src/components/admin/admin-analytics-results.tsx +95 -0
- package/src/components/admin/admin-analytics.tsx +104 -0
- package/src/components/admin/admin-audits.tsx +12 -15
- package/src/components/admin/admin-files.tsx +10 -18
- package/src/components/admin/admin-jobs-executions-panel.tsx +2 -1
- package/src/components/admin/admin-jobs.tsx +3 -1
- package/src/components/admin/admin-keys-token-dialog.tsx +123 -0
- package/src/components/admin/admin-keys.tsx +54 -3
- package/src/components/admin/admin-layout.tsx +107 -0
- package/src/components/admin/admin-payments.tsx +4 -5
- package/src/components/admin/admin-router-options.tsx +130 -0
- package/src/components/admin/admin-router-page.tsx +74 -0
- package/src/components/admin/admin-router.tsx +379 -0
- package/src/components/admin/admin-sessions.tsx +2 -16
- package/src/components/admin/admin-user-cell.tsx +70 -0
- package/src/components/admin/admin-user-detail-overview-tab.tsx +28 -0
- package/src/components/admin/admin-user-detail-profile-policy.spec.ts +146 -0
- package/src/components/admin/admin-user-detail-profile-policy.ts +147 -0
- package/src/components/admin/admin-user-detail-security-tab.tsx +1 -1
- package/src/components/admin/admin-user-detail.tsx +76 -32
- package/src/components/admin/admin-users-status-filter.tsx +2 -0
- package/src/components/admin/admin-workflows-execution-sheet.tsx +277 -0
- package/src/components/admin/admin-workflows-executions-panel.tsx +170 -0
- package/src/components/admin/admin-workflows-status-badge.tsx +40 -0
- package/src/components/admin/admin-workflows.tsx +207 -0
- package/src/components/alepha-table/alepha-table.tsx +73 -17
- package/src/components/app-actions/__tests__/app-actions.browser.spec.tsx +79 -0
- package/src/components/app-actions/app-actions.tsx +113 -0
- package/src/components/app-shell/app-shell.tsx +122 -0
- package/src/components/auth/auth-reset-password.tsx +81 -8
- package/src/components/button-user/button-user.tsx +58 -1
- package/src/components/control/control.tsx +14 -71
- package/src/components/control-password/control-password.tsx +87 -0
- package/src/components/control-select/__tests__/control-select-clear-label.browser.spec.tsx +101 -0
- package/src/components/control-select/__tests__/control-select-deselect.browser.spec.tsx +126 -0
- package/src/components/control-select/__tests__/control-select-searchable.browser.spec.tsx +142 -0
- package/src/components/control-select/control-select.tsx +133 -111
- package/src/components/control-upload/control-upload.tsx +27 -1
- package/src/components/markdown-view/markdown-view.tsx +6 -0
- package/src/components/nav-shell/nav-page.tsx +3 -3
- package/src/components/settings/__tests__/settings-nav.browser.spec.tsx +123 -0
- package/src/components/settings/__tests__/settings-section.browser.spec.tsx +130 -0
- package/src/components/settings/settings-danger-section.tsx +59 -0
- package/src/components/settings/settings-layout.tsx +56 -0
- package/src/components/settings/settings-nav.tsx +131 -0
- package/src/components/settings/settings-row.tsx +81 -0
- package/src/components/settings/settings-section.tsx +64 -0
- package/src/components/ui/alert-dialog.tsx +1 -1
- package/src/components/ui/avatar.tsx +2 -0
- package/src/components/ui/breadcrumb.tsx +1 -1
- package/src/components/ui/calendar.tsx +11 -6
- package/src/components/ui/card.tsx +5 -5
- package/src/components/ui/carousel.tsx +4 -4
- package/src/components/ui/chart.tsx +2 -0
- package/src/components/ui/collapsible.tsx +2 -0
- package/src/components/ui/combobox.tsx +3 -1
- package/src/components/ui/context-menu.tsx +3 -3
- package/src/components/ui/dialog.tsx +6 -1
- package/src/components/ui/direction.tsx +2 -0
- package/src/components/ui/drawer.tsx +134 -37
- package/src/components/ui/dropdown-menu.tsx +13 -3
- package/src/components/ui/empty.tsx +4 -1
- package/src/components/ui/input-otp.tsx +2 -0
- package/src/components/ui/menubar.tsx +2 -2
- package/src/components/ui/pagination.tsx +2 -2
- package/src/components/ui/popover.tsx +2 -0
- package/src/components/ui/radio-group.tsx +2 -0
- package/src/components/ui/scroll-area.tsx +2 -0
- package/src/components/ui/select.tsx +1 -1
- package/src/components/ui/separator.tsx +2 -0
- package/src/components/ui/sheet.tsx +4 -1
- package/src/components/ui/sidebar.tsx +1 -1
- package/src/components/ui/spinner.tsx +1 -0
- package/src/components/ui/switch.tsx +2 -0
- package/src/components/ui/tabs.tsx +2 -0
- package/src/components/ui/toggle-group.tsx +2 -0
- package/src/components/ui/tooltip.tsx +2 -0
- package/src/lib/__tests__/rehype-safe-img.spec.ts +121 -0
- package/src/lib/i18n-fr.ts +68 -0
- package/src/lib/rehype-safe-img.ts +150 -0
- package/src/lib/resize-image.ts +135 -0
- package/src/styles.css +126 -1
- /package/src/components/{admin/admin-user-detail-provider-labels.ts → auth/provider-labels.ts} +0 -0
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { fireEvent, render, screen } from "@testing-library/react";
|
|
2
|
+
import { Alepha } from "alepha";
|
|
3
|
+
import { AlephaContext } from "alepha/react";
|
|
4
|
+
import { AlephaReactI18n } from "alepha/react/i18n";
|
|
5
|
+
import { setupJsdomMocks } from "alepha/react/testing";
|
|
6
|
+
import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";
|
|
7
|
+
import { AdminKeysTokenDialog } from "../admin-keys-token-dialog.tsx";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The dialog is the single moment a freshly minted API key token is readable
|
|
11
|
+
* (the server stores only a hash), so what matters is that the token is
|
|
12
|
+
* actually on screen while set, that nothing renders once cleared, and that
|
|
13
|
+
* both dismissal affordances funnel through `onClose` — a token dialog that
|
|
14
|
+
* cannot close would pin a live credential on screen.
|
|
15
|
+
*/
|
|
16
|
+
describe("AdminKeysTokenDialog", () => {
|
|
17
|
+
let alepha: Alepha | undefined;
|
|
18
|
+
|
|
19
|
+
beforeAll(() => {
|
|
20
|
+
setupJsdomMocks();
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
afterEach(async () => {
|
|
24
|
+
await alepha?.stop();
|
|
25
|
+
alepha = undefined;
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
const mount = async (token: string | null, onClose: () => void) => {
|
|
29
|
+
alepha = Alepha.create().with(AlephaReactI18n);
|
|
30
|
+
await alepha.start();
|
|
31
|
+
return render(
|
|
32
|
+
<AlephaContext.Provider value={alepha}>
|
|
33
|
+
<AdminKeysTokenDialog token={token} onClose={onClose} />
|
|
34
|
+
</AlephaContext.Provider>,
|
|
35
|
+
);
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
it("shows the token and the one-time warning while a token is set", async () => {
|
|
39
|
+
await mount("ak_test_1234567890", () => {});
|
|
40
|
+
|
|
41
|
+
expect(screen.getByText("ak_test_1234567890")).toBeTruthy();
|
|
42
|
+
expect(screen.getByText(/shown only once/)).toBeTruthy();
|
|
43
|
+
// The copy button is icon-only; the aria-label is its accessible name
|
|
44
|
+
// (and doubles as the tooltip text).
|
|
45
|
+
expect(screen.getByRole("button", { name: "Copy" })).toBeTruthy();
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("renders nothing when token is null", async () => {
|
|
49
|
+
await mount(null, () => {});
|
|
50
|
+
|
|
51
|
+
expect(screen.queryByText(/shown only once/)).toBeNull();
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("calls onClose when Done is pressed", async () => {
|
|
55
|
+
const onClose = vi.fn();
|
|
56
|
+
await mount("ak_test_1234567890", onClose);
|
|
57
|
+
|
|
58
|
+
fireEvent.click(screen.getByRole("button", { name: "Done" }));
|
|
59
|
+
|
|
60
|
+
expect(onClose).toHaveBeenCalledTimes(1);
|
|
61
|
+
});
|
|
62
|
+
});
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { render, screen } from "@testing-library/react";
|
|
2
|
+
import { Alepha } from "alepha";
|
|
3
|
+
import { AlephaContext } from "alepha/react";
|
|
4
|
+
import { AlephaReactI18n } from "alepha/react/i18n";
|
|
5
|
+
import { AlephaReactRouter } from "alepha/react/router";
|
|
6
|
+
import { setupJsdomMocks } from "alepha/react/testing";
|
|
7
|
+
import { afterEach, beforeAll, describe, expect, it } from "vitest";
|
|
8
|
+
import AdminLayout from "../admin-layout.tsx";
|
|
9
|
+
import { AdminRouter } from "../admin-router.tsx";
|
|
10
|
+
import {
|
|
11
|
+
type AdminRouterOptions,
|
|
12
|
+
adminRouterOptionsAtom,
|
|
13
|
+
} from "../admin-router-options.tsx";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The three seams `adminRouterOptionsAtom` opens into the layout itself —
|
|
17
|
+
* as opposed to the routing contract, which `admin-router.spec.ts` owns.
|
|
18
|
+
*
|
|
19
|
+
* Each exists for an application whose `/admin` lives inside a document it
|
|
20
|
+
* does not fully own (a host page with fixed overlays, or a hardcoded
|
|
21
|
+
* `<html class="dark">`): `className` is the fence hook, `colorScheme:
|
|
22
|
+
* false` keeps the shell from rewriting the host's theme class, and the ⌘K
|
|
23
|
+
* search affordance surviving a `topbarActions` replacement is what makes
|
|
24
|
+
* replacing the cluster safe — the Spotlight state is local to the layout,
|
|
25
|
+
* so no replacement cluster could ever rebuild the button.
|
|
26
|
+
*/
|
|
27
|
+
describe("AdminLayout options", () => {
|
|
28
|
+
let alepha: Alepha | undefined;
|
|
29
|
+
|
|
30
|
+
beforeAll(() => {
|
|
31
|
+
setupJsdomMocks();
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
afterEach(async () => {
|
|
35
|
+
await alepha?.stop();
|
|
36
|
+
alepha = undefined;
|
|
37
|
+
document.documentElement.className = "";
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
const mount = async (options: AdminRouterOptions) => {
|
|
41
|
+
alepha = Alepha.create().with(AlephaReactRouter).with(AlephaReactI18n);
|
|
42
|
+
alepha.inject(AdminRouter);
|
|
43
|
+
alepha.set(adminRouterOptionsAtom, options);
|
|
44
|
+
await alepha.start();
|
|
45
|
+
return render(
|
|
46
|
+
<AlephaContext.Provider value={alepha}>
|
|
47
|
+
<AdminLayout />
|
|
48
|
+
</AlephaContext.Provider>,
|
|
49
|
+
);
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
it("merges options.className onto the shell root", async () => {
|
|
53
|
+
const { container } = await mount({ className: "admin-fence" });
|
|
54
|
+
|
|
55
|
+
const root = container.firstElementChild;
|
|
56
|
+
expect(root).not.toBeNull();
|
|
57
|
+
expect(root!.classList.contains("admin-fence")).toBe(true);
|
|
58
|
+
// Merged, not replaced — the viewport bound stays.
|
|
59
|
+
expect(root!.classList.contains("h-svh")).toBe(true);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("colorScheme: false leaves the document's theme class alone", async () => {
|
|
63
|
+
// A host document that owns its own theme class, the way a game shell
|
|
64
|
+
// hardcodes `<html class="dark">`. `<ColorScheme />` mounted here would
|
|
65
|
+
// toggle it off (jsdom resolves the color mode light), which is exactly
|
|
66
|
+
// the rewrite the opt-out exists to prevent.
|
|
67
|
+
document.documentElement.classList.add("dark");
|
|
68
|
+
|
|
69
|
+
await mount({ colorScheme: false });
|
|
70
|
+
|
|
71
|
+
expect(document.documentElement.classList.contains("dark")).toBe(true);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("keeps the ⌘K search affordance when topbarActions is replaced", async () => {
|
|
75
|
+
await mount({ topbarActions: <span>custom-cluster</span> });
|
|
76
|
+
|
|
77
|
+
expect(screen.getByText("custom-cluster")).toBeTruthy();
|
|
78
|
+
// The search button is not part of the replaceable cluster: its open
|
|
79
|
+
// state lives in the layout, so replacing the cluster must not cost it.
|
|
80
|
+
expect(screen.getByText("Search…")).toBeTruthy();
|
|
81
|
+
});
|
|
82
|
+
});
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { Alepha } from "alepha";
|
|
2
|
+
import { $page, AlephaReactRouter } from "alepha/react/router";
|
|
3
|
+
import { describe, expect, it } from "vitest";
|
|
4
|
+
import { AdminRouter } from "../admin-router.tsx";
|
|
5
|
+
import { $pageAdmin } from "../admin-router-page.tsx";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* `$pageAdmin` is the one-call form of "a page inside the shared admin shell".
|
|
9
|
+
*
|
|
10
|
+
* It exists because the alternative — injecting `AdminRouter` and writing
|
|
11
|
+
* `parent: this.admin.layout` by hand — puts the rules of the composition
|
|
12
|
+
* nowhere an author will read them.
|
|
13
|
+
*/
|
|
14
|
+
describe("$pageAdmin", () => {
|
|
15
|
+
it("mounts the page under the admin shell without the app wiring a parent", async () => {
|
|
16
|
+
class ShopAdminRouter {
|
|
17
|
+
products = $pageAdmin({
|
|
18
|
+
path: "/products",
|
|
19
|
+
nav: { label: "Catalogue", group: "Commerce", order: 100 },
|
|
20
|
+
component: () => "products",
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const alepha = Alepha.create().with(AlephaReactRouter);
|
|
25
|
+
alepha.with(ShopAdminRouter);
|
|
26
|
+
await alepha.start();
|
|
27
|
+
|
|
28
|
+
const products = alepha
|
|
29
|
+
.primitives($page)
|
|
30
|
+
.find((page) => page.name === "products");
|
|
31
|
+
|
|
32
|
+
expect(products).toBeDefined();
|
|
33
|
+
expect(products!.options.parent).toBe(alepha.inject(AdminRouter).layout);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("registers AdminRouter, so the app never mounts the shell itself", async () => {
|
|
37
|
+
class ShopAdminRouter {
|
|
38
|
+
products = $pageAdmin({
|
|
39
|
+
path: "/products",
|
|
40
|
+
component: () => "products",
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const alepha = Alepha.create().with(AlephaReactRouter);
|
|
45
|
+
alepha.with(ShopAdminRouter);
|
|
46
|
+
await alepha.start();
|
|
47
|
+
|
|
48
|
+
// The app registered only its own class. Declaring one admin page is what
|
|
49
|
+
// brought the shell — and with it the built-in pages — into the container.
|
|
50
|
+
const names = alepha.primitives($page).map((page) => page.name);
|
|
51
|
+
expect(names).toContain("admin");
|
|
52
|
+
expect(names).toContain("users");
|
|
53
|
+
});
|
|
54
|
+
});
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import { $inject, Alepha, OPTIONS } from "alepha";
|
|
2
|
+
import {
|
|
3
|
+
$page,
|
|
4
|
+
AlephaReactRouter,
|
|
5
|
+
type PagePrimitive,
|
|
6
|
+
} from "alepha/react/router";
|
|
7
|
+
import { describe, expect, it } from "vitest";
|
|
8
|
+
import { AdminRouter } from "../admin-router.tsx";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Reads the permission(s) a page's route gate actually enforces, the same
|
|
12
|
+
* way `PagePrimitive.deriveCanFromGuards` does: walk `options.use`, find the
|
|
13
|
+
* `$secure` middleware by its structural name (`alepha/react/router` cannot
|
|
14
|
+
* import `alepha/security` to compare types), and read the permissions off
|
|
15
|
+
* its metadata. Reading it back this way — rather than re-reading
|
|
16
|
+
* `options.permission`, the value a page was built from — is what makes the
|
|
17
|
+
* test catch a page whose `permission` was dropped: the route gate is the
|
|
18
|
+
* thing that would actually change.
|
|
19
|
+
*/
|
|
20
|
+
const permissionsOf = (page: PagePrimitive): string[] => {
|
|
21
|
+
const required: string[] = [];
|
|
22
|
+
for (const middleware of page.options.use ?? []) {
|
|
23
|
+
const metadata = middleware[OPTIONS];
|
|
24
|
+
if (metadata?.name !== "$secure") {
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
const permissions = (
|
|
28
|
+
metadata.options as
|
|
29
|
+
| { permissions?: Array<string | { name: string }> }
|
|
30
|
+
| undefined
|
|
31
|
+
)?.permissions;
|
|
32
|
+
for (const permission of permissions ?? []) {
|
|
33
|
+
required.push(
|
|
34
|
+
typeof permission === "string" ? permission : permission.name,
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return required;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Registering `AdminRouter` must be the whole integration.
|
|
43
|
+
*
|
|
44
|
+
* Paths and names are asserted literally because they are a contract, not an
|
|
45
|
+
* implementation detail. `$secure`'s redirect and `ButtonUser` both push a
|
|
46
|
+
* route called `login`; `NavShell root="admin"` and `Spotlight root="admin"`
|
|
47
|
+
* both anchor on the layout being named `admin`; and an application hanging
|
|
48
|
+
* its own pages off this shell writes `/admin/...` paths against these.
|
|
49
|
+
*
|
|
50
|
+
* `:userId` rather than `:id` is deliberate. Param names must be unique
|
|
51
|
+
* across the whole route table — two routes with different param names at the
|
|
52
|
+
* same position silently lose the inner value — and `:id` is the name an
|
|
53
|
+
* application is most likely to collide with.
|
|
54
|
+
*/
|
|
55
|
+
describe("AdminRouter", () => {
|
|
56
|
+
const mount = async () => {
|
|
57
|
+
const alepha = Alepha.create().with(AlephaReactRouter);
|
|
58
|
+
alepha.inject(AdminRouter);
|
|
59
|
+
await alepha.start();
|
|
60
|
+
return alepha
|
|
61
|
+
.primitives($page)
|
|
62
|
+
.map((page) => ({ path: page.options.path, name: page.name }));
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
it("mounts the shell and its eleven pages", async () => {
|
|
66
|
+
const pages = await mount();
|
|
67
|
+
|
|
68
|
+
expect(pages).toEqual(
|
|
69
|
+
expect.arrayContaining([
|
|
70
|
+
{ path: "/admin", name: "admin" },
|
|
71
|
+
{ path: "/users", name: "users" },
|
|
72
|
+
{ path: "/users/:userId", name: "userDetail" },
|
|
73
|
+
{ path: "/sessions", name: "sessions" },
|
|
74
|
+
{ path: "/keys", name: "keys" },
|
|
75
|
+
{ path: "/jobs", name: "jobs" },
|
|
76
|
+
{ path: "/notifications", name: "notifications" },
|
|
77
|
+
{ path: "/audits", name: "audits" },
|
|
78
|
+
{ path: "/files", name: "files" },
|
|
79
|
+
{ path: "/parameters", name: "parameters" },
|
|
80
|
+
{ path: "/payments", name: "payments" },
|
|
81
|
+
{ path: "/analytics", name: "analytics" },
|
|
82
|
+
]),
|
|
83
|
+
);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("gates every page on its documented permission", async () => {
|
|
87
|
+
const alepha = Alepha.create().with(AlephaReactRouter);
|
|
88
|
+
const router = alepha.inject(AdminRouter);
|
|
89
|
+
await alepha.start();
|
|
90
|
+
|
|
91
|
+
const expected: Array<[PagePrimitive, string[]]> = [
|
|
92
|
+
[router.users, ["admin:user:read"]],
|
|
93
|
+
[router.userDetail, ["admin:user:read"]],
|
|
94
|
+
[router.sessions, ["admin:session:read"]],
|
|
95
|
+
[router.keys, ["admin:api-key:read"]],
|
|
96
|
+
[router.jobs, ["admin:job:read"]],
|
|
97
|
+
[router.notifications, ["admin:notification:read"]],
|
|
98
|
+
[router.audits, ["admin:audit:read"]],
|
|
99
|
+
[router.files, ["admin:file:read"]],
|
|
100
|
+
[router.parameters, ["admin:parameter:read"]],
|
|
101
|
+
[router.payments, ["admin:payment:read", "payments:read"]],
|
|
102
|
+
[router.analytics, ["admin:analytics:read"]],
|
|
103
|
+
];
|
|
104
|
+
|
|
105
|
+
for (const [page, permissions] of expected) {
|
|
106
|
+
expect(permissionsOf(page)).toEqual(permissions);
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it("never uses :id as a param name", async () => {
|
|
111
|
+
const pages = await mount();
|
|
112
|
+
expect(pages.map((it) => it.path)).not.toContain("/users/:id");
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it("lets an application hang its own page off the shell", async () => {
|
|
116
|
+
class ShopAdminRouter {
|
|
117
|
+
protected readonly admin = $inject(AdminRouter);
|
|
118
|
+
|
|
119
|
+
products = $page({
|
|
120
|
+
parent: this.admin.layout,
|
|
121
|
+
path: "/products",
|
|
122
|
+
nav: { label: "Catalogue", group: "Commerce", order: 100 },
|
|
123
|
+
component: () => "products",
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const alepha = Alepha.create().with(AlephaReactRouter);
|
|
128
|
+
alepha.inject(AdminRouter);
|
|
129
|
+
alepha.with(ShopAdminRouter);
|
|
130
|
+
await alepha.start();
|
|
131
|
+
|
|
132
|
+
const products = alepha
|
|
133
|
+
.primitives($page)
|
|
134
|
+
.find((page) => page.name === "products");
|
|
135
|
+
|
|
136
|
+
expect(products).toBeDefined();
|
|
137
|
+
expect(products!.options.parent).toBe(alepha.inject(AdminRouter).layout);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it("hides a page whose backing action the server does not offer", async () => {
|
|
141
|
+
const alepha = Alepha.create().with(AlephaReactRouter);
|
|
142
|
+
const router = alepha.inject(AdminRouter);
|
|
143
|
+
await alepha.start();
|
|
144
|
+
|
|
145
|
+
// Seed the link registry `can` actually consults: `findUsers` is
|
|
146
|
+
// registered, `findAudits` deliberately is not.
|
|
147
|
+
alepha.store.set("alepha.server.request.apiLinks", {
|
|
148
|
+
actions: {
|
|
149
|
+
findUsers: { path: "/users" },
|
|
150
|
+
},
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
const usersCan = router.users.options.can;
|
|
154
|
+
const auditsCan = router.audits.options.can;
|
|
155
|
+
expect(usersCan).toBeDefined();
|
|
156
|
+
expect(auditsCan).toBeDefined();
|
|
157
|
+
|
|
158
|
+
// `has: () => true` is deliberate: it proves the permission side cannot
|
|
159
|
+
// rescue either entry, since `can` now consults `LinkProvider` directly
|
|
160
|
+
// and ignores the context it is called with entirely.
|
|
161
|
+
//
|
|
162
|
+
// The users module is registered: the entry belongs in the sidebar.
|
|
163
|
+
expect(usersCan!({ has: () => true })).toBe(true);
|
|
164
|
+
|
|
165
|
+
// The audits module is not: `findAudits` is absent from the registry, so
|
|
166
|
+
// the entry goes — even though `admin:audit:read` (a permission-shaped
|
|
167
|
+
// name) would be granted here, which is exactly why the permission alone
|
|
168
|
+
// can never be the gate.
|
|
169
|
+
expect(auditsCan!({ has: () => true })).toBe(false);
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it("gates every admin page on an action, not only a permission", async () => {
|
|
173
|
+
const alepha = Alepha.create().with(AlephaReactRouter);
|
|
174
|
+
const router = alepha.inject(AdminRouter);
|
|
175
|
+
await alepha.start();
|
|
176
|
+
|
|
177
|
+
// An empty registry: no action exists for any page to be granted.
|
|
178
|
+
alepha.store.set("alepha.server.request.apiLinks", { actions: {} });
|
|
179
|
+
|
|
180
|
+
const pages = [
|
|
181
|
+
router.users,
|
|
182
|
+
router.userDetail,
|
|
183
|
+
router.sessions,
|
|
184
|
+
router.keys,
|
|
185
|
+
router.jobs,
|
|
186
|
+
router.notifications,
|
|
187
|
+
router.audits,
|
|
188
|
+
router.files,
|
|
189
|
+
router.parameters,
|
|
190
|
+
router.payments,
|
|
191
|
+
router.analytics,
|
|
192
|
+
];
|
|
193
|
+
|
|
194
|
+
for (const page of pages) {
|
|
195
|
+
expect(page.options.can).toBeDefined();
|
|
196
|
+
// Simulate the `*`-wildcard admin from the bug report: every
|
|
197
|
+
// permission-shaped name (colon in it) is granted, but no action name
|
|
198
|
+
// is. A `can` derived only from `permission` (e.g. via `$page`'s
|
|
199
|
+
// `deriveCanFromGuards` fallback) would answer `true` here, since the
|
|
200
|
+
// permission it checks is exactly the kind of name this `has` grants —
|
|
201
|
+
// which is why this must go false, and only does because `can` is
|
|
202
|
+
// wired to a `$client` action independently of `permission`.
|
|
203
|
+
expect(page.options.can!({ has: (name) => name.includes(":") })).toBe(
|
|
204
|
+
false,
|
|
205
|
+
);
|
|
206
|
+
}
|
|
207
|
+
});
|
|
208
|
+
});
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { render, screen } from "@testing-library/react";
|
|
2
|
+
import { Alepha } from "alepha";
|
|
3
|
+
import { AlephaContext } from "alepha/react";
|
|
4
|
+
import { AlephaReactRouter } from "alepha/react/router";
|
|
5
|
+
import { setupJsdomMocks } from "alepha/react/testing";
|
|
6
|
+
import { afterEach, beforeAll, describe, expect, it } from "vitest";
|
|
7
|
+
import { AdminUserCell } from "../admin-user-cell.tsx";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The cell is the single way admin tables name a user, so what it pins is
|
|
11
|
+
* the identifier precedence (email > username > real name > truncated id)
|
|
12
|
+
* and that the label is always a real link to the user detail page — the
|
|
13
|
+
* fallbacks must never silently drop the navigation.
|
|
14
|
+
*/
|
|
15
|
+
describe("AdminUserCell", () => {
|
|
16
|
+
let alepha: Alepha | undefined;
|
|
17
|
+
|
|
18
|
+
beforeAll(() => {
|
|
19
|
+
setupJsdomMocks();
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
afterEach(async () => {
|
|
23
|
+
await alepha?.stop();
|
|
24
|
+
alepha = undefined;
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
const mount = async (ui: React.ReactNode) => {
|
|
28
|
+
alepha = Alepha.create().with(AlephaReactRouter);
|
|
29
|
+
await alepha.start();
|
|
30
|
+
return render(
|
|
31
|
+
<AlephaContext.Provider value={alepha}>{ui}</AlephaContext.Provider>,
|
|
32
|
+
);
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
it("prefers email and links to the user detail page", async () => {
|
|
36
|
+
await mount(
|
|
37
|
+
<AdminUserCell
|
|
38
|
+
userId="0192aaaa-0000-7000-8000-000000000001"
|
|
39
|
+
user={{ email: "owner@example.com", username: "owner" }}
|
|
40
|
+
/>,
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
const link = screen.getByRole("link", { name: "owner@example.com" });
|
|
44
|
+
expect(link.getAttribute("href")).toBe(
|
|
45
|
+
"/admin/users/0192aaaa-0000-7000-8000-000000000001",
|
|
46
|
+
);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("falls back to username, then real name", async () => {
|
|
50
|
+
await mount(
|
|
51
|
+
<AdminUserCell
|
|
52
|
+
userId="0192aaaa-0000-7000-8000-000000000002"
|
|
53
|
+
user={{ firstName: "Ada", lastName: "Lovelace" }}
|
|
54
|
+
/>,
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
expect(screen.getByRole("link", { name: "Ada Lovelace" })).toBeTruthy();
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("keeps the link with a truncated id when the owner no longer resolves", async () => {
|
|
61
|
+
await mount(
|
|
62
|
+
<AdminUserCell userId="0192aaaa-0000-7000-8000-000000000003" />,
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
const link = screen.getByRole("link", { name: "0192aaaa" });
|
|
66
|
+
expect(link.getAttribute("href")).toBe(
|
|
67
|
+
"/admin/users/0192aaaa-0000-7000-8000-000000000003",
|
|
68
|
+
);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("renders a dash without a user id", async () => {
|
|
72
|
+
await mount(<AdminUserCell />);
|
|
73
|
+
|
|
74
|
+
expect(screen.queryByRole("link")).toBeNull();
|
|
75
|
+
expect(screen.getByText("—")).toBeTruthy();
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("uses the snapshot label before the truncated id, still linked", async () => {
|
|
79
|
+
// The audit-log shape: a write-time email snapshot plus the actor id,
|
|
80
|
+
// no live summary.
|
|
81
|
+
await mount(
|
|
82
|
+
<AdminUserCell
|
|
83
|
+
userId="0192aaaa-0000-7000-8000-000000000004"
|
|
84
|
+
fallbackLabel="snapshot@example.com"
|
|
85
|
+
/>,
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
expect(
|
|
89
|
+
screen.getByRole("link", { name: "snapshot@example.com" }),
|
|
90
|
+
).toBeTruthy();
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("renders the snapshot label unlinked when there is no id", async () => {
|
|
94
|
+
await mount(<AdminUserCell fallbackLabel="snapshot@example.com" />);
|
|
95
|
+
|
|
96
|
+
expect(screen.queryByRole("link")).toBeNull();
|
|
97
|
+
expect(screen.getByText("snapshot@example.com")).toBeTruthy();
|
|
98
|
+
});
|
|
99
|
+
});
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { resolveUserDetailId } from "../admin-user-detail.tsx";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* `resolveUserDetailId` is the whole reason `~/git/club/apps/platform` keeps
|
|
6
|
+
* working across a vendored `@alepha/ui` upgrade: it declares its user-detail
|
|
7
|
+
* route as `/users/:id` against this same component, while `AdminRouter`'s
|
|
8
|
+
* own route declares `:userId`. Losing the `:id` fallback would resolve an
|
|
9
|
+
* empty id for that application instead of failing loudly.
|
|
10
|
+
*/
|
|
11
|
+
describe("resolveUserDetailId", () => {
|
|
12
|
+
it("resolves the id from :userId, this router's own param name", () => {
|
|
13
|
+
expect(resolveUserDetailId({ userId: "abc-123" })).toBe("abc-123");
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it("falls back to :id, the name a vendored consumer may still use", () => {
|
|
17
|
+
expect(resolveUserDetailId({ id: "legacy-456" })).toBe("legacy-456");
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("prefers :userId when both are present", () => {
|
|
21
|
+
expect(resolveUserDetailId({ userId: "abc-123", id: "legacy-456" })).toBe(
|
|
22
|
+
"abc-123",
|
|
23
|
+
);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("resolves to an empty string when neither param is present", () => {
|
|
27
|
+
expect(resolveUserDetailId({})).toBe("");
|
|
28
|
+
});
|
|
29
|
+
});
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
|
|
3
|
+
void React;
|
|
4
|
+
|
|
5
|
+
import { AutoForm } from "@alepha/ui/components/auto-form/auto-form";
|
|
6
|
+
import { Badge } from "@alepha/ui/components/ui/badge";
|
|
7
|
+
import { Checkbox } from "@alepha/ui/components/ui/checkbox";
|
|
8
|
+
import { Input } from "@alepha/ui/components/ui/input";
|
|
9
|
+
import { Label } from "@alepha/ui/components/ui/label";
|
|
10
|
+
import { jsonSchemaToZod, type ZObject, z } from "alepha";
|
|
11
|
+
import type {
|
|
12
|
+
AdminAnalyticsController,
|
|
13
|
+
AdminAnalyticsResult,
|
|
14
|
+
AdminDatasetDescriptor,
|
|
15
|
+
} from "alepha/api/analytics";
|
|
16
|
+
import { DateTimeProvider } from "alepha/datetime";
|
|
17
|
+
import { useAlepha, useClient } from "alepha/react";
|
|
18
|
+
import { useForm } from "alepha/react/form";
|
|
19
|
+
import { useI18n } from "alepha/react/i18n";
|
|
20
|
+
import { useMemo, useState } from "react";
|
|
21
|
+
|
|
22
|
+
export interface AdminAnalyticsQueryPanelProps {
|
|
23
|
+
dataset: AdminDatasetDescriptor;
|
|
24
|
+
onResult: (result: AdminAnalyticsResult) => void;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The schema-driven half of the analytics explorer: dimension filters are a
|
|
29
|
+
* zod form rebuilt from the dataset's published JSON Schema
|
|
30
|
+
* (`jsonSchemaToZod`, the same round-trip the parameters admin uses), group-by
|
|
31
|
+
* is the declared dimensions plus the `day` / `hour` pseudo-dimensions, and
|
|
32
|
+
* every measure is summed — the only aggregate the query language has.
|
|
33
|
+
*/
|
|
34
|
+
export function AdminAnalyticsQueryPanel(props: AdminAnalyticsQueryPanelProps) {
|
|
35
|
+
const client = useClient<AdminAnalyticsController>();
|
|
36
|
+
const alepha = useAlepha();
|
|
37
|
+
const { tr } = useI18n();
|
|
38
|
+
|
|
39
|
+
const dimensionNames = Object.keys(props.dataset.dimensions.properties ?? {});
|
|
40
|
+
const measureNames = Object.keys(props.dataset.measures.properties ?? {});
|
|
41
|
+
|
|
42
|
+
const defaultSince = useMemo(() => {
|
|
43
|
+
const dateTime = alepha.inject(DateTimeProvider);
|
|
44
|
+
return new Date(dateTime.nowMillis() - 30 * 86_400_000)
|
|
45
|
+
.toISOString()
|
|
46
|
+
.slice(0, 10);
|
|
47
|
+
}, [alepha]);
|
|
48
|
+
|
|
49
|
+
const [since, setSince] = useState(defaultSince);
|
|
50
|
+
const [groupBy, setGroupBy] = useState<string[]>(["day"]);
|
|
51
|
+
|
|
52
|
+
const filterSchema = useMemo(() => {
|
|
53
|
+
const shape: Record<string, any> = {};
|
|
54
|
+
for (const name of dimensionNames) {
|
|
55
|
+
const property = (props.dataset.dimensions.properties ?? {})[name];
|
|
56
|
+
shape[name] = (jsonSchemaToZod(property) as any).optional();
|
|
57
|
+
}
|
|
58
|
+
return z.object(shape) as ZObject;
|
|
59
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
60
|
+
}, [props.dataset]);
|
|
61
|
+
|
|
62
|
+
const form = useForm(
|
|
63
|
+
{
|
|
64
|
+
schema: filterSchema,
|
|
65
|
+
handler: async (values: Record<string, unknown>) => {
|
|
66
|
+
const where: Record<string, string | number> = {};
|
|
67
|
+
for (const [key, value] of Object.entries(values)) {
|
|
68
|
+
if (value !== undefined && value !== "" && value !== null) {
|
|
69
|
+
where[key] = value as string | number;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
const result = await client.queryDataset({
|
|
73
|
+
params: { name: props.dataset.name },
|
|
74
|
+
body: {
|
|
75
|
+
since,
|
|
76
|
+
where: Object.keys(where).length > 0 ? where : undefined,
|
|
77
|
+
groupBy: groupBy.length > 0 ? groupBy : undefined,
|
|
78
|
+
select: Object.fromEntries(
|
|
79
|
+
measureNames.map((measure) => [measure, "sum" as const]),
|
|
80
|
+
),
|
|
81
|
+
limit: 200,
|
|
82
|
+
},
|
|
83
|
+
});
|
|
84
|
+
props.onResult(result);
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
[props.dataset.name],
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
const toggleGroupBy = (name: string) => {
|
|
91
|
+
setGroupBy((current) =>
|
|
92
|
+
current.includes(name)
|
|
93
|
+
? current.filter((g) => g !== name)
|
|
94
|
+
: [...current, name],
|
|
95
|
+
);
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
return (
|
|
99
|
+
<div className="flex flex-col gap-4">
|
|
100
|
+
<div className="flex flex-wrap items-end gap-6">
|
|
101
|
+
<div className="flex flex-col gap-1.5">
|
|
102
|
+
<Label htmlFor="admin-analytics-since">
|
|
103
|
+
{tr("admin.analytics.since", { default: "Since (UTC day)" })}
|
|
104
|
+
</Label>
|
|
105
|
+
<Input
|
|
106
|
+
id="admin-analytics-since"
|
|
107
|
+
className="w-40"
|
|
108
|
+
value={since}
|
|
109
|
+
onChange={(e) => setSince(e.target.value)}
|
|
110
|
+
/>
|
|
111
|
+
</div>
|
|
112
|
+
<div className="flex flex-col gap-1.5">
|
|
113
|
+
<Label>
|
|
114
|
+
{tr("admin.analytics.groupBy", { default: "Group by" })}
|
|
115
|
+
</Label>
|
|
116
|
+
<div className="flex h-9 flex-wrap items-center gap-3">
|
|
117
|
+
{[...dimensionNames, "day", "hour"].map((name) => (
|
|
118
|
+
<label
|
|
119
|
+
key={name}
|
|
120
|
+
className="flex cursor-pointer items-center gap-1.5 text-sm"
|
|
121
|
+
>
|
|
122
|
+
<Checkbox
|
|
123
|
+
checked={groupBy.includes(name)}
|
|
124
|
+
onCheckedChange={() => toggleGroupBy(name)}
|
|
125
|
+
/>
|
|
126
|
+
{name}
|
|
127
|
+
</label>
|
|
128
|
+
))}
|
|
129
|
+
</div>
|
|
130
|
+
</div>
|
|
131
|
+
<div className="flex h-9 items-center gap-1.5">
|
|
132
|
+
{measureNames.map((measure) => (
|
|
133
|
+
<Badge key={measure} variant="secondary">
|
|
134
|
+
sum({measure})
|
|
135
|
+
</Badge>
|
|
136
|
+
))}
|
|
137
|
+
</div>
|
|
138
|
+
</div>
|
|
139
|
+
<AutoForm
|
|
140
|
+
form={form}
|
|
141
|
+
title={tr("admin.analytics.filters", { default: "Filters" })}
|
|
142
|
+
submitLabel={tr("admin.analytics.run", { default: "Run query" })}
|
|
143
|
+
skipReset
|
|
144
|
+
/>
|
|
145
|
+
</div>
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export default AdminAnalyticsQueryPanel;
|