Skip to content

armgit / hypit

Public Imported from GitHub
0

Clone any viral video with AI agents. Not just a script, the whole workflow: swap the face, the words, the B-roll, ship 100 variants in one command, and get your 100M views.

test: pin the two things every other package's identity rests on

kashorin4d4273f3d32d
diff --git a/packages/protocol/test/canonical.test.ts b/packages/protocol/test/canonical.test.ts
new file mode 100644
index 00000000..e246be8f
--- /dev/null
+++ b/packages/protocol/test/canonical.test.ts
@@ -0,0 +1,127 @@
+import assert from "node:assert/strict";
+import test from "node:test";
+
+import {
+ canonicalize,
+ canonicalStringify,
+ digestOf,
+ isDigest,
+ recordDigest,
+ semanticRecordsDigest,
+} from "@narratage/protocol";
+
+/**
+ * Every identity in the system is a digest of a canonical value: Record
+ * identity, Operation identity, configuration identity, package locks. So these
+ * tests are about one property — two values that mean the same thing must
+ * digest the same, and two that mean different things must not.
+ */
+
+test("key order is not meaning, so it cannot change a digest", () => {
+ assert.equal(
+ digestOf({ a: 1, b: { c: 2, d: 3 } }),
+ digestOf({ b: { d: 3, c: 2 }, a: 1 }),
+ );
+ assert.equal(canonicalStringify({ b: 1, a: 2 }), '{"a":2,"b":1}');
+});
+
+test("array order is meaning, so it does change a digest", () => {
+ assert.notEqual(digestOf([1, 2]), digestOf([2, 1]));
+});
+
+test("a value's type is part of what it means", () => {
+ assert.notEqual(digestOf({ a: 1 }), digestOf({ a: "1" }));
+ assert.notEqual(digestOf({ a: null }), digestOf({ a: false }));
+ assert.notEqual(digestOf({ a: 0 }), digestOf({ a: [] }));
+ assert.notEqual(digestOf([]), digestOf({}));
+});
+
+test("negative zero is zero, because nothing downstream can mean the difference", () => {
+ assert.equal(digestOf({ a: -0 }), digestOf({ a: 0 }));
+ assert.ok(Object.is((canonicalize({ a: -0 }) as { a: number }).a, 0));
+});
+
+test("canonicalizing an already canonical value changes nothing", () => {
+ const value = { z: [1, { y: "x" }], a: null, n: 2.5 };
+ const once = canonicalize(value);
+ assert.deepEqual(canonicalize(once), once);
+ assert.equal(digestOf(once), digestOf(value));
+});
+
+test("a value that cannot mean one thing is refused, and says which path", () => {
+ const refusals: readonly [unknown, RegExp][] = [
+ [{ a: Number.NaN }, /must be finite/u],
+ [{ a: Number.POSITIVE_INFINITY }, /must be finite/u],
+ [{ a: undefined }, /is undefined/u],
+ [{ a: () => 1 }, /contains function/u],
+ [{ a: 1n }, /contains bigint/u],
+ [{ a: Symbol("s") }, /contains symbol/u],
+ [{ a: new Date(0) }, /must be a plain object/u],
+ [{ a: new Map() }, /must be a plain object/u],
+ ];
+ for (const [value, message] of refusals) assert.throws(() => canonicalize(value), message);
+
+ // An accessor could answer differently on a second read, so it is not a value.
+ const accessor = {};
+ Object.defineProperty(accessor, "a", { get: () => 1, enumerable: true });
+ assert.throws(() => canonicalize(accessor), /must be a data property/u);
+});
+
+test("a cycle has no canonical form, in either shape", () => {
+ const object: Record<string, unknown> = {};
+ object.self = object;
+ assert.throws(() => canonicalize(object), /contains a cycle/u);
+ const array: unknown[] = [];
+ array.push(array);
+ assert.throws(() => canonicalize(array), /contains a cycle/u);
+
+ // The same value twice is not a cycle.
+ const shared = { a: 1 };
+ assert.doesNotThrow(() => canonicalize({ left: shared, right: shared }));
+});
+
+test("a digest is recognisable as one", () => {
+ assert.ok(isDigest(digestOf({})));
+ assert.equal(isDigest("sha256:"), false);
+ assert.equal(isDigest("md5:abc"), false);
+ assert.equal(isDigest(`sha256:${"g".repeat(64)}`), false);
+});
+
+test("a Record's identity includes its Type, and a set of Records is order-free", () => {
+ const type = { module: { name: "@x/m", version: "1" }, name: "T" } as const;
+ const other = { module: { name: "@x/m", version: "1" }, name: "U" } as const;
+ const value = { kind: "inline", value: { a: 1 } } as const;
+ assert.notEqual(recordDigest(type, value), recordDigest(other, value));
+
+ const records = [
+ { id: "r1", type, value, conformance: "exact" },
+ { id: "r2", type: other, value, conformance: "exact" },
+ ] as never;
+ const reversed = [records[1], records[0]] as never;
+ assert.equal(semanticRecordsDigest(records), semanticRecordsDigest(reversed));
+});
+
+/**
+ * Pins.
+ *
+ * These are not testing that sha256 works. They exist so that changing how a
+ * value is canonicalized cannot be a quiet change: every archived Build, every
+ * package lock and every recorded Runtime Profile is addressed by these bytes.
+ * Editing a constant here is a deliberate act that a reviewer can see; a
+ * silently different digest is not.
+ */
+test("the canonical form of these values is fixed, and changing it is a visible act", () => {
+ assert.equal(digestOf({}),
+ "sha256:44136fa355b3678a1146ad16f7e8649e94fb4fc21fe77e8310c060f61caaff8a");
+ assert.equal(digestOf([]),
+ "sha256:4f53cda18c2baa0c0354bb5f9a3ecbe5ed12ab4d8e11ba873c2f11161202b945");
+ assert.equal(digestOf({ contract: "svml.example@1", count: 1, ok: true, none: null }),
+ "sha256:2b702b25c218cdbc1f242c1125cee07f648f503af78b0dd48d5f9743f22934c9");
+ // Non-ASCII must not depend on an escaping choice.
+ assert.equal(digestOf({ zh: "口播稿", nested: { a: [1, "1", false] } }),
+ "sha256:96519d6160460ea2b76578b1303fec47e9c3d53b012dce8738a3129126baea59");
+ assert.equal(
+ recordDigest({ module: { name: "@x/m", version: "1" }, name: "T" },
+ { kind: "inline", value: { a: 1 } } as never),
+ "sha256:e231ae28cdbe95ed1fe2d5837e3c4d82e71805ad26b941e8a90c745e4e50cdbc");
+});
diff --git a/packages/runtime-adapter/src/index.ts b/packages/runtime-adapter/src/index.ts
index ed33411d..d90f08cd 100644
--- a/packages/runtime-adapter/src/index.ts
+++ b/packages/runtime-adapter/src/index.ts
@@ -125,11 +125,15 @@ export function createRuntimeEndpointAdapterFacet(options: {
use: options.use,
kind: "endpoint",
})),
- implementation: {
+ // Validated here, not only at the loading boundary. A facet that fails
+ // isRuntimeAdapterHostFacet is skipped by the loader, so the author of a
+ // broken adapter would otherwise learn about it as "adapter X is not
+ // registered", three layers from the mistake.
+ implementation: implementation({
create: options.create,
...(options.doctor === undefined ? {} : { doctor: options.doctor }),
...(options.service === undefined ? {} : { service: options.service }),
- },
+ }, `Runtime Adapter ${options.use}`),
};
return facet;
}
@@ -146,10 +150,10 @@ export function createRuntimeServiceAdapterFacet(options: {
use: options.use,
kind: "runtime-service",
})),
- implementation: {
+ implementation: implementation({
create: options.create,
...(options.doctor === undefined ? {} : { doctor: options.doctor }),
- },
+ }, `Runtime Adapter ${options.use}`),
};
}
diff --git a/packages/runtime-adapter/test/registry.test.ts b/packages/runtime-adapter/test/registry.test.ts
new file mode 100644
index 00000000..88b6d951
--- /dev/null
+++ b/packages/runtime-adapter/test/registry.test.ts
@@ -0,0 +1,124 @@
+import assert from "node:assert/strict";
+import test from "node:test";
+
+import { digestOf } from "@narratage/protocol";
+
+import {
+ createRuntimeEndpointAdapterFacet,
+ createRuntimeServiceAdapterFacet,
+ isRuntimeAdapterHostFacet,
+ runtimeConfigBoolean,
+ runtimeConfigExact,
+ runtimeConfigObject,
+ runtimeConfigPositiveInteger,
+ runtimeConfigString,
+ RuntimeAdapterRegistry,
+} from "@narratage/runtime-adapter";
+
+const context = { root: "/tmp", instance: "one", config: {} };
+const endpoint = (use: string, extra: Record<string, unknown> = {}) =>
+ createRuntimeEndpointAdapterFacet({ use, create: () => ({ use }) as never, ...extra } as never);
+
+test("a facet declares which kind it is, and the registry keeps the two apart", () => {
+ const registry = new RuntimeAdapterRegistry();
+ registry.registerFacet(endpoint("example.endpoint"));
+ registry.registerFacet(createRuntimeServiceAdapterFacet({
+ use: "example.service",
+ create: () => ({}) as never,
+ }));
+
+ assert.ok(registry.has("example.endpoint", "endpoint"));
+ assert.equal(registry.has("example.endpoint", "runtime-service"), false);
+ assert.ok(registry.has("example.service", "runtime-service"));
+ assert.equal(registry.has("example.service", "endpoint"), false);
+ assert.equal(registry.has("example.absent"), false);
+});
+
+test("asking an Endpoint adapter for a Runtime Service is refused, not coerced", async () => {
+ const registry = new RuntimeAdapterRegistry();
+ registry.registerFacet(endpoint("example.endpoint"));
+ await assert.rejects(async () => await registry.createService("example.endpoint", context));
+ await assert.rejects(async () => await registry.createEndpoint("example.absent", context));
+});
+
+test("one name is one adapter, so a second registration is an error rather than a winner", () => {
+ const registry = new RuntimeAdapterRegistry();
+ registry.registerFacet(endpoint("example.endpoint"));
+ assert.throws(() => registry.registerFacet(endpoint("example.endpoint")), /already registered/u);
+});
+
+test("a facet without create() is not a facet", () => {
+ assert.throws(
+ () => createRuntimeEndpointAdapterFacet({ use: "x" } as never),
+ /does not implement create\(\)/u,
+ );
+ assert.throws(
+ () => createRuntimeEndpointAdapterFacet({ use: "x", create: () => ({}), doctor: 1 } as never),
+ /doctor must be a function/u,
+ );
+ assert.throws(() => createRuntimeEndpointAdapterFacet({ use: " ", create: () => ({}) } as never), /use name is empty/u);
+});
+
+test("an external service is present only when the adapter declares one", () => {
+ const registry = new RuntimeAdapterRegistry();
+ registry.registerFacet(endpoint("example.bare"));
+ registry.registerFacet(endpoint("example.declaring", {
+ service: () => ({ id: "thing", probe: async () => ({ state: "ready" as const }) }),
+ }));
+ assert.equal(registry.service("example.bare", context), undefined);
+ assert.equal(registry.service("example.declaring", context)?.id, "thing");
+});
+
+test("an adapter with no doctor reports nothing rather than failing", async () => {
+ const registry = new RuntimeAdapterRegistry();
+ registry.registerFacet(endpoint("example.bare"));
+ registry.registerFacet(endpoint("example.diagnosing", {
+ doctor: () => [{ severity: "warning" as const, code: "X", message: "m" }],
+ }));
+ assert.deepEqual(await registry.doctor("example.bare", "endpoint", context), []);
+ assert.equal((await registry.doctor("example.diagnosing", "endpoint", context)).length, 1);
+});
+
+test("a produced facet is recognisable as a Runtime Adapter host facet", () => {
+ assert.ok(isRuntimeAdapterHostFacet(endpoint("example.endpoint")));
+ assert.equal(isRuntimeAdapterHostFacet({ abi: "something-else" } as never), false);
+});
+
+test("Runtime config readers accept a value or refuse it; they never guess one", () => {
+ const config = runtimeConfigObject({ a: "x", n: 2, b: true }, "subject");
+ assert.equal(runtimeConfigString(config.a, "a"), "x");
+ assert.equal(runtimeConfigPositiveInteger(config.n, "n"), 2);
+ assert.equal(runtimeConfigBoolean(config.b, "b"), true);
+ // Absence is absence, not a default.
+ assert.equal(runtimeConfigString(undefined, "a"), undefined);
+ assert.equal(runtimeConfigPositiveInteger(undefined, "n"), undefined);
+ assert.equal(runtimeConfigBoolean(undefined, "b"), undefined);
+
+ assert.throws(() => runtimeConfigObject([] as never, "subject"), /must be an object/u);
+ assert.throws(() => runtimeConfigString(1 as never, "a"), /a/u);
+ assert.throws(() => runtimeConfigPositiveInteger(0 as never, "n"), /n/u);
+ assert.throws(() => runtimeConfigPositiveInteger(1.5 as never, "n"), /n/u);
+ assert.throws(() => runtimeConfigBoolean("true" as never, "b"), /b/u);
+ // An unknown key is a misconfiguration, not something to ignore.
+ assert.throws(() => runtimeConfigExact(config, ["a", "n"], "subject"), /does not accept b/u);
+ assert.doesNotThrow(() => runtimeConfigExact(config, ["a", "n", "b"], "subject"));
+});
+
+/**
+ * Pin.
+ *
+ * A package lock records a `facetsDigest` derived from these identities. If the
+ * identity of an unchanged adapter shifted, every recorded lock would fail
+ * verification for a reason nobody changed on purpose — which has happened once
+ * already, when `kind` was renamed. The constant makes that a visible edit.
+ */
+test("an unchanged adapter keeps its identity, because locks are addressed by it", () => {
+ const facet = endpoint("@narratage/example-provider");
+ assert.deepEqual(facet.identity, {
+ contract: "svml.runtime-adapter-facet@1",
+ use: "@narratage/example-provider",
+ kind: "endpoint",
+ });
+ assert.equal(digestOf(facet.identity),
+ "sha256:c9ce6e18b3d02f65148cfc06b016ae0a7dee00430558bb393472726fb91a756f");
+});