diff --git a/src/application/binaryDiagnostics/AppleIpsService.test.ts b/src/application/binaryDiagnostics/AppleIpsService.test.ts index d0c168965fa..69df4b2456a 100644 --- a/src/application/binaryDiagnostics/AppleIpsService.test.ts +++ b/src/application/binaryDiagnostics/AppleIpsService.test.ts @@ -1,16 +1,13 @@ +import { lateCancellationEvidence } from "../../../tests/support/lateCancellationEvidence.js"; import { createHash } from "node:crypto"; import { parseAppleIps } from "../../native/apple/AppleIpsParser.js"; import { inspectAppleIpsInputSchema } from "../../domain/native/appleIps.js"; -import { parseEvidence } from "../../domain/evidence.js"; import { expect, it } from "vitest"; import { AppleIpsService } from "./AppleIpsService.js"; import { APPLE_IPS_PROVIDER } from "../../domain/investigationProviders.js"; import type { AppleIps } from "../../domain/native/appleIps.js"; import { ok } from "../../domain/result.js"; -import { - AnalysisCancelledError, - AnalysisInputError, -} from "../../domain/analysisErrorCore.js"; +import { AnalysisInputError } from "../../domain/analysisErrorCore.js"; const identity = APPLE_IPS_PROVIDER; const bytes = Buffer.from( @@ -204,23 +201,12 @@ it.each([ ); it("retains a completed observation when the caller cancels as the provider completes", async () => { - const controller = new AbortController(); - const reason = { request: "late AppleIps cancellation" }; - const service = new AppleIpsService({ - identity, - inspect: () => { - controller.abort(reason); - return Promise.resolve(ok(report)); - }, - }); - const result = await service.inspect( - { path: report.artifact.path }, - { signal: controller.signal }, + const evidence = await lateCancellationEvidence( + report, + "late AppleIps cancellation", + (inspect) => new AppleIpsService({ identity, inspect }), ); - if (result.ok) throw new Error("Expected late cancellation"); - expect(result.error).toBeInstanceOf(AnalysisCancelledError); - expect(result.error.cause).toBe(reason); - expect(parseEvidence(result.error.partialObservation)).toMatchObject({ + expect(evidence).toMatchObject({ operation: "inspect_apple_ips", normalized_result: report, }); diff --git a/src/application/binaryDiagnostics/ElfDebugLinkService.test.ts b/src/application/binaryDiagnostics/ElfDebugLinkService.test.ts index 688441a3065..b241f917920 100644 --- a/src/application/binaryDiagnostics/ElfDebugLinkService.test.ts +++ b/src/application/binaryDiagnostics/ElfDebugLinkService.test.ts @@ -1,3 +1,4 @@ +import { lateCancellationEvidence } from "../../../tests/support/lateCancellationEvidence.js"; import { resolvedAnalysisError } from "../../domain/analysisErrorBase.js"; import { analysisErrorWithCleanupFailure } from "../../domain/analysisErrorCleanup.js"; import { ProviderCleanupError } from "../../domain/providerCleanupError.js"; @@ -308,27 +309,12 @@ it("rejects an unreadable-candidate verdict without its limitation", async () => }); it("retains a completed observation if the caller cancels while the provider completes", async () => { - const controller = new AbortController(); - const reason = { request: "late elf debug link cancellation" }; - const service = new ElfDebugLinkService({ - identity, - inspect: () => { - controller.abort(reason); - return Promise.resolve(ok(report)); - }, - }); - const late = await service.inspect( - { path: report.artifact.path }, - { signal: controller.signal }, + const evidence = await lateCancellationEvidence( + report, + "late elf debug link cancellation", + (inspect) => new ElfDebugLinkService({ identity, inspect }), ); - expect(late).toMatchObject({ - ok: false, - error: { _tag: "AnalysisCancelledError" }, - }); - if (late.ok) throw new Error("Expected late cancellation"); - expect(late.error).toBeInstanceOf(AnalysisCancelledError); - expect(late.error.cause).toBe(reason); - expect(parseEvidence(late.error.partialObservation)).toMatchObject({ + expect(evidence).toMatchObject({ operation: "inspect_elf_debug_link", confidence: "observed", subject: { identity: { format: "elf", architecture: "x86_64" } }, diff --git a/src/application/binaryDiagnostics/ElfDependenciesService.test.ts b/src/application/binaryDiagnostics/ElfDependenciesService.test.ts index e12a066d7ee..fbc0cd724e4 100644 --- a/src/application/binaryDiagnostics/ElfDependenciesService.test.ts +++ b/src/application/binaryDiagnostics/ElfDependenciesService.test.ts @@ -1,11 +1,10 @@ +import { lateCancellationEvidence } from "../../../tests/support/lateCancellationEvidence.js"; import { expect, it } from "vitest"; import { ElfDependenciesService } from "./ElfDependenciesService.js"; import { ELF_DEPENDENCIES_PROVIDER } from "../../domain/investigationProviders.js"; import type { ElfDependencies } from "../../domain/native/elfDependencies.js"; import { ok } from "../../domain/result.js"; -import { parseEvidence } from "../../domain/evidence.js"; import { - AnalysisCancelledError, AnalysisInputError, AnalysisOutputError, } from "../../domain/analysisErrorCore.js"; @@ -148,27 +147,12 @@ it("rejects complete coverage that still retains table issues", async () => { }); it("retains a completed observation if the caller cancels while the provider completes", async () => { - const controller = new AbortController(); - const reason = { request: "late ELF dependency cancellation" }; - const service = new ElfDependenciesService({ - identity, - inspect: () => { - controller.abort(reason); - return Promise.resolve(ok(report)); - }, - }); - const late = await service.inspect( - { path: report.artifact.path }, - { signal: controller.signal }, + const evidence = await lateCancellationEvidence( + report, + "late ELF dependency cancellation", + (inspect) => new ElfDependenciesService({ identity, inspect }), ); - expect(late).toMatchObject({ - ok: false, - error: { _tag: "AnalysisCancelledError" }, - }); - if (late.ok) throw new Error("Expected late cancellation"); - expect(late.error).toBeInstanceOf(AnalysisCancelledError); - expect(late.error.cause).toBe(reason); - expect(parseEvidence(late.error.partialObservation)).toMatchObject({ + expect(evidence).toMatchObject({ operation: "inspect_elf_dependencies", confidence: "observed", subject: { diff --git a/src/application/binaryDiagnostics/ElfNotesService.test.ts b/src/application/binaryDiagnostics/ElfNotesService.test.ts index f87306f2d53..501b9e437b7 100644 --- a/src/application/binaryDiagnostics/ElfNotesService.test.ts +++ b/src/application/binaryDiagnostics/ElfNotesService.test.ts @@ -1,4 +1,4 @@ -import { parseEvidence } from "../../domain/evidence.js"; +import { lateCancellationEvidence } from "../../../tests/support/lateCancellationEvidence.js"; import { expect, it } from "vitest"; import { ElfNotesService } from "./ElfNotesService.js"; import { ELF_NOTES_PROVIDER } from "../../domain/investigationProviders.js"; @@ -108,23 +108,12 @@ it("retains an examined empty table and rejects an uncounted table", async () => }); it("retains a completed observation when the caller cancels as the provider completes", async () => { - const controller = new AbortController(); - const reason = { request: "late ElfNotes cancellation" }; - const service = new ElfNotesService({ - identity, - inspect: () => { - controller.abort(reason); - return Promise.resolve(ok(report)); - }, - }); - const result = await service.inspect( - { path: report.artifact.path }, - { signal: controller.signal }, + const evidence = await lateCancellationEvidence( + report, + "late ElfNotes cancellation", + (inspect) => new ElfNotesService({ identity, inspect }), ); - if (result.ok) throw new Error("Expected late cancellation"); - expect(result.error).toBeInstanceOf(AnalysisCancelledError); - expect(result.error.cause).toBe(reason); - expect(parseEvidence(result.error.partialObservation)).toMatchObject({ + expect(evidence).toMatchObject({ operation: "inspect_elf_notes", normalized_result: report, }); diff --git a/src/application/binaryDiagnostics/ElfUnwindRangesService.test.ts b/src/application/binaryDiagnostics/ElfUnwindRangesService.test.ts index f6518774a14..cb0b08f3f04 100644 --- a/src/application/binaryDiagnostics/ElfUnwindRangesService.test.ts +++ b/src/application/binaryDiagnostics/ElfUnwindRangesService.test.ts @@ -1,4 +1,4 @@ -import { parseEvidence } from "../../domain/evidence.js"; +import { lateCancellationEvidence } from "../../../tests/support/lateCancellationEvidence.js"; import { expect, it } from "vitest"; import { ElfUnwindRangesService } from "./ElfUnwindRangesService.js"; import { ELF_UNWIND_RANGES_PROVIDER } from "../../domain/investigationProviders.js"; @@ -15,7 +15,6 @@ import { } from "../../native/elf/ElfUnwindTables.fixture.js"; import { ok } from "../../domain/result.js"; import { - AnalysisCancelledError, AnalysisInputError, AnalysisOutputError, } from "../../domain/analysisErrorCore.js"; @@ -369,23 +368,12 @@ it("rejects a report that fails the output schema", async () => { }); it("retains a completed observation when the caller cancels as the provider completes", async () => { - const controller = new AbortController(); - const reason = { request: "late ElfUnwindRanges cancellation" }; - const service = new ElfUnwindRangesService({ - identity, - inspect: () => { - controller.abort(reason); - return Promise.resolve(ok(report)); - }, - }); - const result = await service.inspect( - { path: report.artifact.path }, - { signal: controller.signal }, + const evidence = await lateCancellationEvidence( + report, + "late ElfUnwindRanges cancellation", + (inspect) => new ElfUnwindRangesService({ identity, inspect }), ); - if (result.ok) throw new Error("Expected late cancellation"); - expect(result.error).toBeInstanceOf(AnalysisCancelledError); - expect(result.error.cause).toBe(reason); - expect(parseEvidence(result.error.partialObservation)).toMatchObject({ + expect(evidence).toMatchObject({ operation: "inspect_elf_unwind_ranges", normalized_result: report, }); diff --git a/src/application/binaryDiagnostics/KernelModuleService.test.ts b/src/application/binaryDiagnostics/KernelModuleService.test.ts index a34fd598617..2d5c96449ce 100644 --- a/src/application/binaryDiagnostics/KernelModuleService.test.ts +++ b/src/application/binaryDiagnostics/KernelModuleService.test.ts @@ -1,11 +1,10 @@ +import { lateCancellationEvidence } from "../../../tests/support/lateCancellationEvidence.js"; import { expect, it } from "vitest"; import { KernelModuleService } from "./KernelModuleService.js"; import { KERNEL_MODULE_PROVIDER } from "../../domain/investigationProviders.js"; import type { KernelModule } from "../../domain/native/kernelModule.js"; import { ok } from "../../domain/result.js"; -import { parseEvidence } from "../../domain/evidence.js"; import { - AnalysisCancelledError, AnalysisInputError, AnalysisOutputError, } from "../../domain/analysisErrorCore.js"; @@ -201,27 +200,12 @@ it.each([ ); it("retains a completed observation if the caller cancels while the provider completes", async () => { - const controller = new AbortController(); - const reason = { request: "late kernel module cancellation" }; - const service = new KernelModuleService({ - identity, - inspect: () => { - controller.abort(reason); - return Promise.resolve(ok(report)); - }, - }); - const late = await service.inspect( - { path: report.artifact.path }, - { signal: controller.signal }, + const evidence = await lateCancellationEvidence( + report, + "late kernel module cancellation", + (inspect) => new KernelModuleService({ identity, inspect }), ); - expect(late).toMatchObject({ - ok: false, - error: { _tag: "AnalysisCancelledError" }, - }); - if (late.ok) throw new Error("Expected late cancellation"); - expect(late.error).toBeInstanceOf(AnalysisCancelledError); - expect(late.error.cause).toBe(reason); - expect(parseEvidence(late.error.partialObservation)).toMatchObject({ + expect(evidence).toMatchObject({ operation: "inspect_kernel_module", confidence: "observed", subject: { identity: { format: "elf", architecture: "x86_64" } }, diff --git a/src/application/binaryDiagnostics/NativeInitializersService.test.ts b/src/application/binaryDiagnostics/NativeInitializersService.test.ts index 4daeacb8518..1b9b99c7a16 100644 --- a/src/application/binaryDiagnostics/NativeInitializersService.test.ts +++ b/src/application/binaryDiagnostics/NativeInitializersService.test.ts @@ -1,11 +1,10 @@ -import { parseEvidence } from "../../domain/evidence.js"; +import { lateCancellationEvidence } from "../../../tests/support/lateCancellationEvidence.js"; import { expect, it } from "vitest"; import { NativeInitializersService } from "./NativeInitializersService.js"; import { NATIVE_INITIALIZERS_PROVIDER } from "../../domain/investigationProviders.js"; import type { NativeInitializers } from "../../domain/native/nativeInitializers.js"; import { ok } from "../../domain/result.js"; import { - AnalysisCancelledError, AnalysisInputError, AnalysisOutputError, } from "../../domain/analysisErrorCore.js"; @@ -179,23 +178,12 @@ it("rejects a slot offset outside the artifact", async () => { }); it("retains a completed observation when the caller cancels as the provider completes", async () => { - const controller = new AbortController(); - const reason = { request: "late native initializer cancellation" }; - const service = new NativeInitializersService({ - identity, - inspect: () => { - controller.abort(reason); - return Promise.resolve(ok(report)); - }, - }); - const result = await service.inspect( - { path: report.artifact.path }, - { signal: controller.signal }, + const evidence = await lateCancellationEvidence( + report, + "late native initializer cancellation", + (inspect) => new NativeInitializersService({ identity, inspect }), ); - if (result.ok) throw new Error("Expected late cancellation"); - expect(result.error).toBeInstanceOf(AnalysisCancelledError); - expect(result.error.cause).toBe(reason); - expect(parseEvidence(result.error.partialObservation)).toMatchObject({ + expect(evidence).toMatchObject({ operation: "inspect_native_initializers", normalized_result: report, }); diff --git a/src/application/binaryDiagnostics/PeCertificatesService.test.ts b/src/application/binaryDiagnostics/PeCertificatesService.test.ts index e1264077a11..8cf03440553 100644 --- a/src/application/binaryDiagnostics/PeCertificatesService.test.ts +++ b/src/application/binaryDiagnostics/PeCertificatesService.test.ts @@ -1,4 +1,4 @@ -import { parseEvidence } from "../../domain/evidence.js"; +import { lateCancellationEvidence } from "../../../tests/support/lateCancellationEvidence.js"; import { createHash } from "node:crypto"; import { inspectPeCertificatesInputSchema } from "../../domain/native/peCertificates.js"; import { @@ -12,7 +12,6 @@ import { PE_CERTIFICATES_PROVIDER } from "../../domain/investigationProviders.js import type { PeCertificates } from "../../domain/native/peCertificates.js"; import { ok } from "../../domain/result.js"; import { - AnalysisCancelledError, AnalysisInputError, AnalysisOutputError, } from "../../domain/analysisErrorCore.js"; @@ -52,23 +51,12 @@ it("rejects a relative path and extra arguments before reading", async () => { }); it("retains a completed observation when the caller cancels as the provider completes", async () => { - const controller = new AbortController(); - const reason = { request: "late PeCertificates cancellation" }; - const service = new PeCertificatesService({ - identity, - inspect: () => { - controller.abort(reason); - return Promise.resolve(ok(report)); - }, - }); - const result = await service.inspect( - { path: report.artifact.path }, - { signal: controller.signal }, + const evidence = await lateCancellationEvidence( + report, + "late PeCertificates cancellation", + (inspect) => new PeCertificatesService({ identity, inspect }), ); - if (result.ok) throw new Error("Expected late cancellation"); - expect(result.error).toBeInstanceOf(AnalysisCancelledError); - expect(result.error.cause).toBe(reason); - expect(parseEvidence(result.error.partialObservation)).toMatchObject({ + expect(evidence).toMatchObject({ operation: "inspect_pe_certificates", normalized_result: report, }); diff --git a/src/application/binaryDiagnostics/PeImportsService.test.ts b/src/application/binaryDiagnostics/PeImportsService.test.ts index 840f42d2186..f2c2bcbccc9 100644 --- a/src/application/binaryDiagnostics/PeImportsService.test.ts +++ b/src/application/binaryDiagnostics/PeImportsService.test.ts @@ -1,3 +1,4 @@ +import { lateCancellationEvidence } from "../../../tests/support/lateCancellationEvidence.js"; import { createHash } from "node:crypto"; import { peImportImage, @@ -9,13 +10,11 @@ import { peImportsSchema, type PeImports, } from "../../domain/native/peImports.js"; -import { parseEvidence } from "../../domain/evidence.js"; import { expect, it } from "vitest"; import { PeImportsService } from "./PeImportsService.js"; import { PE_IMPORTS_PROVIDER } from "../../domain/investigationProviders.js"; import { ok } from "../../domain/result.js"; import { - AnalysisCancelledError, AnalysisInputError, AnalysisOutputError, } from "../../domain/analysisErrorCore.js"; @@ -109,23 +108,12 @@ it.each(["import_directory", "delay_import_directory"] as const)( ); it("retains a completed observation when the caller cancels as the provider completes", async () => { - const controller = new AbortController(); - const reason = { request: "late PeImports cancellation" }; - const service = new PeImportsService({ - identity, - inspect: () => { - controller.abort(reason); - return Promise.resolve(ok(report)); - }, - }); - const result = await service.inspect( - { path: report.artifact.path }, - { signal: controller.signal }, + const evidence = await lateCancellationEvidence( + report, + "late PeImports cancellation", + (inspect) => new PeImportsService({ identity, inspect }), ); - if (result.ok) throw new Error("Expected late cancellation"); - expect(result.error).toBeInstanceOf(AnalysisCancelledError); - expect(result.error.cause).toBe(reason); - expect(parseEvidence(result.error.partialObservation)).toMatchObject({ + expect(evidence).toMatchObject({ operation: "inspect_pe_imports", normalized_result: report, }); diff --git a/src/application/binaryDiagnostics/WindowsShortcutService.test.ts b/src/application/binaryDiagnostics/WindowsShortcutService.test.ts index f49aecee790..11e9585b5ee 100644 --- a/src/application/binaryDiagnostics/WindowsShortcutService.test.ts +++ b/src/application/binaryDiagnostics/WindowsShortcutService.test.ts @@ -1,4 +1,4 @@ -import { parseEvidence } from "../../domain/evidence.js"; +import { lateCancellationEvidence } from "../../../tests/support/lateCancellationEvidence.js"; import { expect, it } from "vitest"; import { WindowsShortcutService } from "./WindowsShortcutService.js"; import { WINDOWS_SHORTCUT_PROVIDER } from "../../domain/investigationProviders.js"; @@ -7,7 +7,6 @@ import { inspectWindowsShortcutInputSchema } from "../../domain/native/windowsSh import { parseWindowsShortcut } from "../../native/windows/WindowsShortcutParser.js"; import { ok } from "../../domain/result.js"; import { - AnalysisCancelledError, AnalysisInputError, AnalysisOutputError, } from "../../domain/analysisErrorCore.js"; @@ -103,23 +102,12 @@ it("rejects complete coverage that still lists issues", async () => { }); it("retains a completed observation when the caller cancels as the provider completes", async () => { - const controller = new AbortController(); - const reason = { request: "late WindowsShortcut cancellation" }; - const service = new WindowsShortcutService({ - identity, - inspect: () => { - controller.abort(reason); - return Promise.resolve(ok(report)); - }, - }); - const result = await service.inspect( - { path: report.artifact.path }, - { signal: controller.signal }, + const evidence = await lateCancellationEvidence( + report, + "late WindowsShortcut cancellation", + (inspect) => new WindowsShortcutService({ identity, inspect }), ); - if (result.ok) throw new Error("Expected late cancellation"); - expect(result.error).toBeInstanceOf(AnalysisCancelledError); - expect(result.error.cause).toBe(reason); - expect(parseEvidence(result.error.partialObservation)).toMatchObject({ + expect(evidence).toMatchObject({ operation: "inspect_windows_shortcut", normalized_result: report, }); diff --git a/tests/support/lateCancellationEvidence.ts b/tests/support/lateCancellationEvidence.ts new file mode 100644 index 00000000000..0f23a896596 --- /dev/null +++ b/tests/support/lateCancellationEvidence.ts @@ -0,0 +1,34 @@ +import { expect } from "vitest"; +import type { AnalysisError } from "../../src/domain/analysisErrorBase.js"; +import { AnalysisCancelledError } from "../../src/domain/analysisErrorCore.js"; +import { parseEvidence, type Evidence } from "../../src/domain/evidence.js"; +import { ok, type Result } from "../../src/domain/result.js"; + +export const lateCancellationEvidence = async < + Report extends { artifact: { path: string } }, +>( + report: Report, + request: string, + createService: (inspect: () => Promise>) => { + inspect( + input: { path: string }, + options: { signal: AbortSignal }, + ): Promise>; + }, +): Promise => { + const controller = new AbortController(); + const reason = { request }; + const service = createService(() => { + controller.abort(reason); + return Promise.resolve(ok(report)); + }); + const result = await service.inspect( + { path: report.artifact.path }, + { signal: controller.signal }, + ); + if (result.ok) throw new Error("Expected late cancellation"); + expect(result.error).toBeInstanceOf(AnalysisCancelledError); + expect(result.error._tag).toBe("AnalysisCancelledError"); + expect(result.error.cause).toBe(reason); + return parseEvidence(result.error.partialObservation); +};