mirror of
https://github.com/morluto/rea.git
synced 2026-12-02 19:57:58 +00:00
90 lines
3.0 KiB
TypeScript
90 lines
3.0 KiB
TypeScript
import { createHash } from "node:crypto";
|
|
import { expect, vi } from "vitest";
|
|
|
|
import type { AnalysisError } from "../../src/domain/analysisErrorBase.js";
|
|
import { projectAnalysisError } from "../../src/domain/analysisErrorProjection.js";
|
|
import type { Evidence } from "../../src/domain/evidence.js";
|
|
import type { Result } from "../../src/domain/result.js";
|
|
import { OwnedFileHandle } from "../../src/filesystem/OwnedFileHandle.js";
|
|
|
|
export const fileSnapshotFinalizationModes = [
|
|
"control",
|
|
"close-failure",
|
|
"close-cancel",
|
|
"close-both",
|
|
] as const;
|
|
|
|
export const expectFileSnapshotFinalization = async (
|
|
mode: (typeof fileSnapshotFinalizationModes)[number],
|
|
createService: () => {
|
|
inspect(
|
|
input: { path: string },
|
|
options: { signal: AbortSignal },
|
|
): Promise<Result<Evidence, AnalysisError>>;
|
|
close(): Promise<void>;
|
|
},
|
|
target: { path: string; bytes: Uint8Array },
|
|
expectedResult: Readonly<Record<string, unknown>>,
|
|
): Promise<void> => {
|
|
const { path, bytes } = target;
|
|
const controller = new AbortController();
|
|
const reason = { request: "cancel during snapshot close" };
|
|
const closeFailure = Object.assign(
|
|
new Error("controlled snapshot close EIO"),
|
|
{
|
|
code: "EIO",
|
|
},
|
|
);
|
|
const cancel = mode === "close-cancel" || mode === "close-both";
|
|
const failedClose = mode === "close-failure" || mode === "close-both";
|
|
const original = OwnedFileHandle.prototype.close;
|
|
let owner: OwnedFileHandle | undefined;
|
|
const close = vi
|
|
.spyOn(OwnedFileHandle.prototype, "close")
|
|
.mockImplementationOnce(async function (this: OwnedFileHandle) {
|
|
owner = this;
|
|
if (cancel) controller.abort(reason);
|
|
if (failedClose) throw closeFailure;
|
|
await original.call(this);
|
|
});
|
|
const service = createService();
|
|
try {
|
|
const result = await service.inspect(
|
|
{ path },
|
|
{ signal: controller.signal },
|
|
);
|
|
if (mode === "control") {
|
|
if (!result.ok) throw result.error;
|
|
expect(result.value.normalized_result).toMatchObject(expectedResult);
|
|
} else {
|
|
if (result.ok) throw new Error("Expected finalization failure");
|
|
const acquired = {
|
|
kind: "file-snapshot-hash",
|
|
path,
|
|
sha256: createHash("sha256").update(bytes).digest("hex"),
|
|
bytes: bytes.length,
|
|
};
|
|
expect(result.error.partialObservation).toEqual(acquired);
|
|
expect(
|
|
projectAnalysisError(result.error).details?.partial_observation,
|
|
).toEqual(acquired);
|
|
expect(result.error.cleanupIncomplete).toBe(failedClose);
|
|
if (failedClose) expect(owner?.handle.fd).toBeGreaterThanOrEqual(0);
|
|
if (cancel) {
|
|
const primary = result.error.cleanupPrimary ?? result.error;
|
|
expect(primary).toMatchObject({
|
|
_tag: "AnalysisCancelledError",
|
|
cause: reason,
|
|
});
|
|
}
|
|
close.mockRestore();
|
|
await service.close();
|
|
expect(result.error.cleanupIncomplete).toBe(false);
|
|
}
|
|
expect(owner?.handle.fd).toBe(-1);
|
|
} finally {
|
|
close.mockRestore();
|
|
await service.close();
|
|
}
|
|
};
|