feat(custom-widgets): complete Custom JSX v2 (#6390)
This commit is contained in:
1 parent
3a1245cbc5
commit
acdfcbb18e
416 files changed
+37853
-18066
No files matched your search
@@ -1,72 +1,112 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { customWidgetDefinitionSchema } from "../core";
|
||||
import { CustomWidgetPreviewSessionService } from "../server";
|
||||
import type { CreatePreviewSessionInput } from "../server";
|
||||
|
||||
function createHarness() {
|
||||
let now = 1_000;
|
||||
let id = 0;
|
||||
const service = new CustomWidgetPreviewSessionService({
|
||||
createId: () => `id-${++id}`,
|
||||
encrypt: (value) => `encrypted:${value}`,
|
||||
decrypt: (value) => value.replace("encrypted:", ""),
|
||||
now: () => now,
|
||||
});
|
||||
return {
|
||||
service,
|
||||
advance: (milliseconds: number) => {
|
||||
now += milliseconds;
|
||||
},
|
||||
};
|
||||
}
|
||||
const definition = customWidgetDefinitionSchema.parse({
|
||||
$schema: "homarr-custom-widget-v2",
|
||||
name: "Preview",
|
||||
sources: { default: { baseUrl: "https://example.com", networkScope: "public", auth: "bearer" } },
|
||||
requests: { data: { path: "/data" } },
|
||||
options: { limit: { label: "Limit", control: "number", default: 10 } },
|
||||
template: "<Text>{data.data?.name}</Text>",
|
||||
});
|
||||
|
||||
const input: CreatePreviewSessionInput = {
|
||||
userId: "user-1",
|
||||
baseUrl: "https://example.com/api",
|
||||
authType: "bearer",
|
||||
secrets: [{ kind: "apiKey", value: "secret" }],
|
||||
networkScope: "public",
|
||||
requests: [],
|
||||
};
|
||||
|
||||
describe("preview session service", () => {
|
||||
it("stores secrets encrypted and returns them only through the decrypting boundary", async () => {
|
||||
const { service } = createHarness();
|
||||
const created = await service.create(input);
|
||||
const session = await service.get(created.id, input.userId);
|
||||
expect(session.secrets[0]?.value).toBe("encrypted:secret");
|
||||
expect(service.getSecrets(session)).toEqual([{ kind: "apiKey", value: "secret" }]);
|
||||
describe("preview sessions", () => {
|
||||
it("stores keyed definitions and encrypted secrets", async () => {
|
||||
let id = 0;
|
||||
const service = new CustomWidgetPreviewSessionService({
|
||||
createId: () => `id-${++id}`,
|
||||
encrypt: (value) => `encrypted:${value}`,
|
||||
decrypt: (value) => value.replace("encrypted:", ""),
|
||||
now: () => 1_000,
|
||||
});
|
||||
const created = await service.create({
|
||||
userId: "user",
|
||||
sources: definition.sources,
|
||||
requests: definition.requests,
|
||||
name: definition.name,
|
||||
template: definition.template,
|
||||
optionDefinitions: definition.options,
|
||||
options: { limit: 10 },
|
||||
secrets: [{ sourceId: "default", kind: "apiKey", value: "secret" }],
|
||||
});
|
||||
const session = await service.get(created.id, "user");
|
||||
expect(session.requests.data?.path).toBe("/data");
|
||||
expect(session.secrets[0]?.value).not.toBe("secret");
|
||||
expect(service.getSecrets(session, "default")).toEqual([{ kind: "apiKey", value: "secret" }]);
|
||||
});
|
||||
|
||||
it("isolates sessions by user and expires them after five minutes", async () => {
|
||||
const { service, advance } = createHarness();
|
||||
const created = await service.create(input);
|
||||
await expect(service.get(created.id, "other-user")).rejects.toMatchObject({ code: "NOT_FOUND" });
|
||||
const second = await service.create(input);
|
||||
advance(5 * 60_000 + 1);
|
||||
await expect(service.get(second.id, input.userId)).rejects.toMatchObject({ code: "NOT_FOUND" });
|
||||
it("keeps journal paths redacted and bounded to the session", async () => {
|
||||
let id = 0;
|
||||
const service = new CustomWidgetPreviewSessionService({
|
||||
createId: () => `id-${++id}`,
|
||||
encrypt: String,
|
||||
decrypt: String,
|
||||
});
|
||||
const created = await service.create({
|
||||
userId: "user",
|
||||
sources: definition.sources,
|
||||
requests: definition.requests,
|
||||
name: definition.name,
|
||||
template: definition.template,
|
||||
optionDefinitions: definition.options,
|
||||
options: { limit: 10 },
|
||||
secrets: [],
|
||||
});
|
||||
const session = await service.get(created.id, "user");
|
||||
await service.appendJournal(session, {
|
||||
requestId: "data",
|
||||
kind: "query",
|
||||
method: "GET",
|
||||
path: "/data",
|
||||
status: 200,
|
||||
durationMs: 5,
|
||||
simulated: false,
|
||||
});
|
||||
expect((await service.getJournal(created.id, "user"))[0]).toMatchObject({ path: "/data", status: 200 });
|
||||
});
|
||||
|
||||
it("updates live-action approval and caps the newest-first journal", async () => {
|
||||
const { service } = createHarness();
|
||||
const created = await service.create(input);
|
||||
const approved = await service.setLiveActions(created.id, input.userId, true);
|
||||
expect(approved.liveActions).toBe(true);
|
||||
const session = await service.get(created.id, input.userId);
|
||||
for (let index = 0; index < 52; index += 1) {
|
||||
await service.appendJournal(session, {
|
||||
requestId: `request-${index}`,
|
||||
kind: "query",
|
||||
method: "GET",
|
||||
pathTemplate: "/data",
|
||||
status: 200,
|
||||
durationMs: 1,
|
||||
simulated: false,
|
||||
});
|
||||
}
|
||||
const journal = await service.getJournal(created.id, input.userId);
|
||||
expect(journal).toHaveLength(50);
|
||||
expect(journal[0]?.requestId).toBe("request-51");
|
||||
expect(journal.at(-1)?.requestId).toBe("request-2");
|
||||
it("applies source deployment values without restoring stale source metadata", async () => {
|
||||
let id = 0;
|
||||
const service = new CustomWidgetPreviewSessionService({
|
||||
createId: () => `id-${++id}`,
|
||||
encrypt: String,
|
||||
decrypt: String,
|
||||
});
|
||||
const created = await service.create({
|
||||
userId: "user",
|
||||
sources: definition.sources,
|
||||
requests: definition.requests,
|
||||
name: definition.name,
|
||||
template: definition.template,
|
||||
optionDefinitions: definition.options,
|
||||
options: { limit: 10 },
|
||||
secrets: [],
|
||||
});
|
||||
|
||||
await service.configureSource(
|
||||
created.id,
|
||||
"user",
|
||||
"default",
|
||||
{ baseUrl: "http://service.local", networkScope: "private", auth: "bearer" },
|
||||
[{ sourceId: "default", kind: "apiKey", value: "secret" }],
|
||||
);
|
||||
|
||||
const configured = await service.get(created.id, "user");
|
||||
expect(configured.sources.default).toEqual({
|
||||
baseUrl: "http://service.local",
|
||||
networkScope: "private",
|
||||
auth: "bearer",
|
||||
});
|
||||
await expect(
|
||||
service.configureSource(
|
||||
created.id,
|
||||
"user",
|
||||
"default",
|
||||
{ baseUrl: "http://service.local", networkScope: "private", auth: "none" },
|
||||
[],
|
||||
),
|
||||
).rejects.toThrow("authentication changed");
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user