Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/packages/plugin-runtime/src/manifest.test.ts
T
2026-07-08 16:15:37 +02:00

296 lines
9.9 KiB
TypeScript

import { describe, expect, test } from "vitest";
import { manifestDeclaresCapability, parsePluginManifest, PluginManifestSchema } from "./manifest.js";
const hex64 = "0".repeat(64);
const hex128 = "0".repeat(128);
const settingsHtmlSha256 = "56a78618698dbd72399e9d07b4f12a9e7910cd4a1b7de41a2f90bd4a33ea338e";
const settingsHtmlIntegrity = "sha256-VqeGGGmNvXI5np0HtPEqnnkQzUobfeQaL5C9SjPqM44=";
const validManifest = {
schemaVersion: 1,
id: "de.hivegrid.hello",
version: "1.0.0",
name: "Hello",
entry: "plugin.wasm",
wasmSha256: hex64,
signature: hex128,
signedBy: hex64,
allowedHosts: ["api.example.com"],
capabilities: [{ name: "log" as const }, { name: "http.outbound" as const, scope: { allowedHosts: ["api.example.com"] } }],
hooks: [{ name: "onHello", export: "handleHello" }],
};
describe("PluginManifestSchema", () => {
test("accepts a well-formed manifest", () => {
const parsed = PluginManifestSchema.safeParse(validManifest);
expect(parsed.success).toBe(true);
});
test("rejects reverse-DNS-shaped ids that have only one label", () => {
const parsed = PluginManifestSchema.safeParse({ ...validManifest, id: "hello" });
expect(parsed.success).toBe(false);
});
test("rejects non-semver versions", () => {
const parsed = PluginManifestSchema.safeParse({ ...validManifest, version: "v1" });
expect(parsed.success).toBe(false);
});
test("rejects unknown capability names (closed enum)", () => {
const parsed = PluginManifestSchema.safeParse({
...validManifest,
capabilities: [{ name: "filesystem.write" }],
});
expect(parsed.success).toBe(false);
});
test("rejects schemaVersion other than 1", () => {
const parsed = PluginManifestSchema.safeParse({ ...validManifest, schemaVersion: 2 });
expect(parsed.success).toBe(false);
});
test("rejects signatures that are not 128 hex chars", () => {
const parsed = PluginManifestSchema.safeParse({ ...validManifest, signature: "abc" });
expect(parsed.success).toBe(false);
});
test("rejects manifests with no hooks", () => {
const parsed = PluginManifestSchema.safeParse({ ...validManifest, hooks: [] });
expect(parsed.success).toBe(false);
});
test("rejects unknown top-level fields (strict mode)", () => {
const parsed = PluginManifestSchema.safeParse({ ...validManifest, surpriseField: true });
expect(parsed.success).toBe(false);
});
test("rejects hosts with a scheme prefix", () => {
const parsed = PluginManifestSchema.safeParse({
...validManifest,
allowedHosts: ["https://api.example.com"],
});
expect(parsed.success).toBe(false);
});
test("rejects localhost, IP, and private-ish outbound hosts", () => {
for (const host of ["localhost", "api.localhost", "127.0.0.1", "service.local", "db.internal"]) {
const parsed = PluginManifestSchema.safeParse({
...validManifest,
allowedHosts: [host],
});
expect(parsed.success, host).toBe(false);
}
});
test("rejects private-ish hosts inside capability scopes", () => {
const parsed = PluginManifestSchema.safeParse({
...validManifest,
capabilities: [{ name: "http.outbound", scope: { allowedHosts: ["127.0.0.1"] } }],
});
expect(parsed.success).toBe(false);
});
test("rejects duplicate and oversized top-level allowed host lists", () => {
expect(
PluginManifestSchema.safeParse({
...validManifest,
allowedHosts: ["api.example.com", "api.example.com"],
}).success
).toBe(false);
expect(
PluginManifestSchema.safeParse({
...validManifest,
allowedHosts: Array.from({ length: 65 }, (_, index) => `api-${index}.example.com`),
}).success
).toBe(false);
});
test("rejects duplicate and oversized scoped allowed host lists", () => {
expect(
PluginManifestSchema.safeParse({
...validManifest,
capabilities: [{ name: "http.outbound", scope: { allowedHosts: ["api.example.com", "api.example.com"] } }],
}).success
).toBe(false);
expect(
PluginManifestSchema.safeParse({
...validManifest,
capabilities: [{ name: "http.outbound", scope: { allowedHosts: Array.from({ length: 33 }, (_, index) => `api-${index}.example.com`) } }],
}).success
).toBe(false);
});
test("rejects duplicate and oversized capability declarations", () => {
expect(
PluginManifestSchema.safeParse({
...validManifest,
capabilities: [{ name: "log" }, { name: "log" }],
}).success
).toBe(false);
expect(
PluginManifestSchema.safeParse({
...validManifest,
capabilities: Array.from({ length: 33 }, () => ({ name: "log" as const })),
}).success
).toBe(false);
});
test("rejects duplicate and oversized hook declarations", () => {
expect(
PluginManifestSchema.safeParse({
...validManifest,
hooks: [
{ name: "entity.created", export: "handleCreated" },
{ name: "entity.created", export: "handleCreatedAgain" },
],
}).success
).toBe(false);
expect(
PluginManifestSchema.safeParse({
...validManifest,
hooks: [
{ name: "entity.created", export: "handleCreated" },
{ name: "entity.updated", export: "handleCreated" },
],
}).success
).toBe(false);
expect(
PluginManifestSchema.safeParse({
...validManifest,
hooks: Array.from({ length: 65 }, (_, index) => ({ name: `hook.${index}`, export: `handle${index}` })),
}).success
).toBe(false);
});
// F-017 — host-version constraint fields are now schema-enforced.
test("accepts optional minHostVersion / maxHostVersion when omitted", () => {
const parsed = PluginManifestSchema.safeParse(validManifest);
expect(parsed.success).toBe(true);
if (parsed.success) {
expect(parsed.data.minHostVersion).toBeUndefined();
expect(parsed.data.maxHostVersion).toBeUndefined();
}
});
test("accepts well-formed minHostVersion / maxHostVersion SemVer values", () => {
const parsed = PluginManifestSchema.safeParse({
...validManifest,
minHostVersion: "1.0.0",
maxHostVersion: "2.0.0-rc.1",
});
expect(parsed.success).toBe(true);
if (parsed.success) {
expect(parsed.data.minHostVersion).toBe("1.0.0");
expect(parsed.data.maxHostVersion).toBe("2.0.0-rc.1");
}
});
test("rejects minHostVersion that is not loose-SemVer", () => {
const parsed = PluginManifestSchema.safeParse({
...validManifest,
minHostVersion: "v1",
});
expect(parsed.success).toBe(false);
});
test("rejects maxHostVersion that is not loose-SemVer", () => {
const parsed = PluginManifestSchema.safeParse({
...validManifest,
maxHostVersion: "latest",
});
expect(parsed.success).toBe(false);
});
test("accepts an optional settings UI bundle entry", () => {
const parsed = PluginManifestSchema.safeParse({
...validManifest,
ui: { settings: { entry: "settings/index.html", sha256: settingsHtmlSha256 } },
});
expect(parsed.success).toBe(true);
if (parsed.success) {
expect(parsed.data.ui?.settings?.entry).toBe("settings/index.html");
expect(parsed.data.ui?.settings?.sha256).toBe(settingsHtmlSha256);
}
});
test("accepts settings UI SRI integrity and rejects missing or mismatched integrity metadata", () => {
expect(
PluginManifestSchema.safeParse({
...validManifest,
ui: { settings: { entry: "settings/index.html", integrity: settingsHtmlIntegrity } },
}).success
).toBe(true);
expect(
PluginManifestSchema.safeParse({
...validManifest,
ui: { settings: { entry: "settings/index.html" } },
}).success
).toBe(false);
expect(
PluginManifestSchema.safeParse({
...validManifest,
ui: { settings: { entry: "settings/index.html", sha256: settingsHtmlSha256, integrity: `sha256-${"A".repeat(43)}=` } },
}).success
).toBe(false);
});
test("rejects unsafe settings UI entries and unknown ui fields", () => {
for (const entry of ["/settings.html", "../settings.html", "settings/../index.html", "https://example.com/settings.html", "settings\\index.html", "settings/index.js"]) {
const parsed = PluginManifestSchema.safeParse({
...validManifest,
ui: { settings: { entry } },
});
expect(parsed.success, entry).toBe(false);
}
expect(
PluginManifestSchema.safeParse({
...validManifest,
ui: { settings: { entry: "settings.html", sha256: settingsHtmlSha256 }, surprise: true },
}).success
).toBe(false);
});
});
describe("parsePluginManifest", () => {
test("returns ok=true with the typed manifest on success", () => {
const result = parsePluginManifest(validManifest);
expect(result.ok).toBe(true);
if (result.ok) {
expect(result.manifest.id).toBe("de.hivegrid.hello");
expect(result.manifest.capabilities).toHaveLength(2);
}
});
test("aggregates Zod issues without throwing", () => {
const result = parsePluginManifest({ schemaVersion: 1 });
expect(result.ok).toBe(false);
if (!result.ok) {
expect(result.issues.length).toBeGreaterThan(0);
}
});
});
describe("manifestDeclaresCapability", () => {
test("returns true when the capability is in the list", () => {
const result = parsePluginManifest(validManifest);
if (!result.ok) throw new Error("fixture invalid");
expect(manifestDeclaresCapability(result.manifest, "http.outbound")).toBe(true);
expect(manifestDeclaresCapability(result.manifest, "log")).toBe(true);
});
test("returns false when the capability is not declared", () => {
const result = parsePluginManifest(validManifest);
if (!result.ok) throw new Error("fixture invalid");
expect(manifestDeclaresCapability(result.manifest, "db.write")).toBe(false);
expect(manifestDeclaresCapability(result.manifest, "secret.read")).toBe(false);
});
});