Initial Commit
This commit is contained in:
@@ -0,0 +1,157 @@
|
||||
import { trace } from "@opentelemetry/api";
|
||||
import { createStorageUriResolver } from "@suaveplan/storage-uri";
|
||||
import { beforeEach, describe, expect, it } from "@suaveplan/testing/runner";
|
||||
import { asDrive, FakeDrive } from "../../../tests/setup/fake-drive.js";
|
||||
import {
|
||||
DRIVE_URI_CODES,
|
||||
DriveUriError,
|
||||
} from "../../error-codes/error-codes.js";
|
||||
import { driveMeshUriPlugin } from "./drive-mesh.js";
|
||||
|
||||
interface SpanLike {
|
||||
setAttribute(name: string, value: unknown): SpanLike;
|
||||
end(): void;
|
||||
readonly attributes: Map<string, unknown>;
|
||||
}
|
||||
|
||||
function makeRecordingSpan(): SpanLike {
|
||||
const attrs = new Map<string, unknown>();
|
||||
return {
|
||||
attributes: attrs,
|
||||
setAttribute(name: string, value: unknown): SpanLike {
|
||||
attrs.set(name, value);
|
||||
return this;
|
||||
},
|
||||
end(): void {},
|
||||
};
|
||||
}
|
||||
|
||||
describe("driveMeshUriPlugin", () => {
|
||||
let fake: FakeDrive;
|
||||
|
||||
beforeEach(() => {
|
||||
fake = new FakeDrive({
|
||||
contents: new Map([["raw/clip.mov", new Uint8Array([1, 2, 3])]]),
|
||||
});
|
||||
});
|
||||
|
||||
it("requires options.drive at construction", { timeout: 5000 }, () => {
|
||||
expect(() =>
|
||||
driveMeshUriPlugin({
|
||||
drive: undefined as unknown as Parameters<
|
||||
typeof driveMeshUriPlugin
|
||||
>[0]["drive"],
|
||||
})
|
||||
).toThrow(DriveUriError);
|
||||
});
|
||||
|
||||
it("rejects non-mesh schemes", { timeout: 5000 }, () => {
|
||||
const plugin = driveMeshUriPlugin({ drive: asDrive(fake) });
|
||||
try {
|
||||
plugin.parse("drive://primary/x");
|
||||
} catch (err) {
|
||||
expect(err).toBeInstanceOf(DriveUriError);
|
||||
expect((err as DriveUriError).code).toBe(
|
||||
DRIVE_URI_CODES.INVALID_URI
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
it("rejects URIs without an authority", { timeout: 5000 }, () => {
|
||||
const plugin = driveMeshUriPlugin({ drive: asDrive(fake) });
|
||||
expect(() => plugin.parse("drive+mesh:///x")).toThrow(DriveUriError);
|
||||
});
|
||||
|
||||
it("rejects URIs without a path", { timeout: 5000 }, () => {
|
||||
const plugin = driveMeshUriPlugin({ drive: asDrive(fake) });
|
||||
expect(() => plugin.parse("drive+mesh://swarmA/")).toThrow(
|
||||
DriveUriError
|
||||
);
|
||||
});
|
||||
|
||||
it("rejects URIs with no authority component (no //)", {
|
||||
timeout: 5000,
|
||||
}, () => {
|
||||
const plugin = driveMeshUriPlugin({ drive: asDrive(fake) });
|
||||
// `drive+mesh:x` has scheme + path but no authority — covers the
|
||||
// `parts.authority ?? ""` fallback branch.
|
||||
expect(() => plugin.parse("drive+mesh:x")).toThrow(DriveUriError);
|
||||
});
|
||||
|
||||
it("dispatches a drive+mesh:// URI to the configured Drive", {
|
||||
timeout: 5000,
|
||||
}, async () => {
|
||||
const plugin = driveMeshUriPlugin({ drive: asDrive(fake) });
|
||||
const resolver = createStorageUriResolver([plugin]);
|
||||
const bytes = await resolver.read("drive+mesh://swarmA/raw/clip.mov");
|
||||
expect(bytes).toEqual(new Uint8Array([1, 2, 3]));
|
||||
expect(fake.calls.read).toHaveLength(1);
|
||||
expect(fake.calls.read[0]?.path).toBe("raw/clip.mov");
|
||||
});
|
||||
|
||||
it("surfaces swarmId on the emitted span", { timeout: 5000 }, () => {
|
||||
const span = makeRecordingSpan();
|
||||
const plugin = driveMeshUriPlugin({ drive: asDrive(fake) });
|
||||
// Patch the OTel API's getActiveSpan to return our recording span
|
||||
// for the duration of this test. Without an SDK-installed context
|
||||
// manager, `trace.setSpan(context, span)` is a no-op (the noop
|
||||
// context manager doesn't propagate); patching at the API layer is
|
||||
// the lightest-weight test seam compatible with `@opentelemetry/api`
|
||||
// alone (no `@opentelemetry/sdk-trace-base` dev dependency needed).
|
||||
const originalGetActiveSpan = trace.getActiveSpan.bind(trace);
|
||||
trace.getActiveSpan = (() =>
|
||||
span as unknown as ReturnType<
|
||||
typeof trace.getActiveSpan
|
||||
>) as typeof trace.getActiveSpan;
|
||||
try {
|
||||
plugin.parse("drive+mesh://swarmA/raw/clip.mov?peers=secret");
|
||||
} finally {
|
||||
trace.getActiveSpan = originalGetActiveSpan;
|
||||
}
|
||||
expect(span.attributes.get("drive.uri.swarm_id")).toBe("swarmA");
|
||||
expect(span.attributes.get("drive.uri.scheme")).toBe("drive+mesh");
|
||||
expect(span.attributes.get("drive.uri")).toBe(
|
||||
"drive+mesh://swarmA/raw/clip.mov"
|
||||
);
|
||||
});
|
||||
|
||||
it("does not attempt span attribute writes when no span is active", {
|
||||
timeout: 5000,
|
||||
}, () => {
|
||||
const plugin = driveMeshUriPlugin({ drive: asDrive(fake) });
|
||||
// No active span — must not throw.
|
||||
expect(() =>
|
||||
plugin.parse("drive+mesh://swarmA/raw/clip.mov")
|
||||
).not.toThrow();
|
||||
});
|
||||
|
||||
it("build round-trips swarmId and key into a drive+mesh:// URI", {
|
||||
timeout: 5000,
|
||||
}, () => {
|
||||
const plugin = driveMeshUriPlugin({ drive: asDrive(fake) });
|
||||
expect(plugin.build?.("raw/clip.mov", { swarmId: "swarmA" })).toBe(
|
||||
"drive+mesh://swarmA/raw/clip.mov"
|
||||
);
|
||||
});
|
||||
|
||||
it("build defaults swarmId to options.defaultSwarmId when meta is omitted", {
|
||||
timeout: 5000,
|
||||
}, () => {
|
||||
const plugin = driveMeshUriPlugin({
|
||||
drive: asDrive(fake),
|
||||
defaultSwarmId: "fallback",
|
||||
});
|
||||
expect(plugin.build?.("raw/clip.mov")).toBe(
|
||||
"drive+mesh://fallback/raw/clip.mov"
|
||||
);
|
||||
});
|
||||
|
||||
it("build uses 'default' when neither meta nor defaultSwarmId is provided", {
|
||||
timeout: 5000,
|
||||
}, () => {
|
||||
const plugin = driveMeshUriPlugin({ drive: asDrive(fake) });
|
||||
expect(plugin.build?.("clip.mov")).toBe(
|
||||
"drive+mesh://default/clip.mov"
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user