mirror of
https://github.com/morluto/rea.git
synced 2026-12-03 21:46:01 +00:00
test(javascript): consolidate artifact graph and resolution coverage
This commit is contained in:
1 parent
b2af0d77e0
commit
1ff7be8c70
22 files changed
+595
-1590
No files matched your search
@@ -1,6 +1,6 @@
|
||||
import { addElectronIpcBoundaries } from "./ElectronBoundaryGraphIpc.js";
|
||||
import { createElectronIpcContext } from "./ElectronBoundaryAnalysis.js";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { expect, it } from "vitest";
|
||||
import { createHash } from "node:crypto";
|
||||
|
||||
import type { ArtifactInventorySnapshot } from "../../domain/artifactInventorySnapshot.js";
|
||||
@@ -80,7 +80,6 @@ const analysisFor = (
|
||||
});
|
||||
|
||||
const contextFor = (
|
||||
file: JavaScriptArtifactFile,
|
||||
analysis: JavaScriptArtifactAnalysis,
|
||||
): JavaScriptArtifactGraphContext => {
|
||||
const accumulator = new JavaScriptArtifactGraphAccumulator();
|
||||
@@ -97,11 +96,12 @@ const contextFor = (
|
||||
// remain on the individual files and do not imply missing inventory paths.
|
||||
coverage: { scope: "root", status: "complete" },
|
||||
} as unknown as ArtifactInventorySnapshot;
|
||||
const files = analysis.files.map(({ file }) => file);
|
||||
const fileSet = {
|
||||
files: [file],
|
||||
files,
|
||||
unavailable_relevant_files: [],
|
||||
containers: [],
|
||||
text_bytes_read: file.bytes,
|
||||
text_bytes_read: files.reduce((total, { bytes }) => total + bytes, 0),
|
||||
invalid_utf8_files: 0,
|
||||
};
|
||||
return {
|
||||
@@ -111,7 +111,7 @@ const contextFor = (
|
||||
analysis,
|
||||
directoryPaths: new Set<string>(),
|
||||
root: createJavaScriptArtifactRootNode(accumulator, snapshot),
|
||||
filesByPath: new Map([[file.path, file]]),
|
||||
filesByPath: new Map(files.map((entry) => [entry.path, entry])),
|
||||
unanalyzedFiles: undefined,
|
||||
fileNodes: new Map(),
|
||||
assetNodes: new Map(),
|
||||
@@ -127,68 +127,54 @@ const contextFor = (
|
||||
};
|
||||
};
|
||||
|
||||
const fileObservation = (context: JavaScriptArtifactGraphContext) => {
|
||||
const node = context.accumulator
|
||||
it("distinguishes interrupted JavaScript analysis from unattempted analysis", () => {
|
||||
const interrupted = skippedFile();
|
||||
const text = "export const ignored = true;";
|
||||
const unattempted: JavaScriptArtifactFile = {
|
||||
...interrupted,
|
||||
path: "unattempted.js",
|
||||
sha256: createHash("sha256").update(text).digest("hex"),
|
||||
bytes: Buffer.byteLength(text),
|
||||
text: { included: true, value: text },
|
||||
};
|
||||
const analysis = analysisFor([
|
||||
skippedEntry(interrupted),
|
||||
{ file: unattempted, javascript: null, semantic: null },
|
||||
]);
|
||||
const context = contextFor(analysis);
|
||||
addJavaScriptArtifactFiles(context);
|
||||
|
||||
const observations = context.accumulator
|
||||
.nodes()
|
||||
.find(({ kind }) => kind === "javascript-asset");
|
||||
if (node === undefined) throw new Error("Expected a javascript-asset node");
|
||||
const observation = node.observations[0];
|
||||
if (observation === undefined) throw new Error("Expected a file observation");
|
||||
return observation.properties;
|
||||
};
|
||||
|
||||
describe("skipped JavaScript parse reporting", () => {
|
||||
it("marks parse_status skipped for a JavaScript file whose worker analysis failed", () => {
|
||||
const file = skippedFile();
|
||||
const context = contextFor(file, analysisFor([skippedEntry(file)]));
|
||||
addJavaScriptArtifactFiles(context);
|
||||
expect(fileObservation(context).parse_status).toBe("skipped");
|
||||
expect(fileObservation(context).analysis_coverage).toMatchObject({
|
||||
syntax: { status: "not-examined", error_count: null },
|
||||
semantics: { unknown_findings: null },
|
||||
examination: { status: "not-examined" },
|
||||
.filter(({ kind }) => kind === "javascript-asset")
|
||||
.flatMap(({ observations }) => observations);
|
||||
expect(observations).toHaveLength(2);
|
||||
for (const [path, status, reason] of [
|
||||
["main.js", "skipped", "JavaScript analysis did not complete."],
|
||||
["unattempted.js", null, "JavaScript source analysis was not performed."],
|
||||
] as const) {
|
||||
const observation = observations.find(
|
||||
({ properties }) => properties.path === path,
|
||||
);
|
||||
expect(observation?.properties).toMatchObject({
|
||||
parse_status: status,
|
||||
analysis_coverage: {
|
||||
syntax: { status: "not-examined", error_count: null },
|
||||
semantics: { unknown_findings: null },
|
||||
examination: { status: "not-examined" },
|
||||
unexamined_reason: reason,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("keeps parse_status null for a JavaScript file never attempted", () => {
|
||||
const file = skippedFile();
|
||||
const entry: AnalyzedJavaScriptArtifactFile = {
|
||||
file,
|
||||
javascript: null,
|
||||
semantic: null,
|
||||
};
|
||||
const context = contextFor(file, analysisFor([entry]));
|
||||
addJavaScriptArtifactFiles(context);
|
||||
expect(fileObservation(context).parse_status).toBeNull();
|
||||
});
|
||||
|
||||
it("keeps the parsed status for an analyzed JavaScript file", () => {
|
||||
const file = skippedFile();
|
||||
const javascript = analyzeJavaScriptStaticSource(
|
||||
"const {ipcMain}=require('electron');\n",
|
||||
);
|
||||
const entry: AnalyzedJavaScriptArtifactFile = {
|
||||
file,
|
||||
javascript,
|
||||
semantic: null,
|
||||
};
|
||||
const context = contextFor(file, analysisFor([entry]));
|
||||
addJavaScriptArtifactFiles(context);
|
||||
expect(fileObservation(context).parse_status).toBe(javascript.parse_status);
|
||||
expect(fileObservation(context).analysis_coverage).toEqual(
|
||||
javascript.analysis_coverage,
|
||||
);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
it("preserves omitted inventory facts on an unavailable artifact", () => {
|
||||
const file = skippedFile();
|
||||
const base = contextFor(file, analysisFor([]));
|
||||
const base = contextFor(analysisFor([]));
|
||||
const context: JavaScriptArtifactGraphContext = {
|
||||
...base,
|
||||
fileSet: {
|
||||
...base.fileSet,
|
||||
files: [],
|
||||
unavailable_relevant_files: [
|
||||
{
|
||||
path: file.path,
|
||||
@@ -260,13 +246,7 @@ it.each([false, true])(
|
||||
},
|
||||
{ file: page, javascript: null, semantic: null },
|
||||
]);
|
||||
const base = contextFor(window, analysis);
|
||||
const files = [setter, window, page];
|
||||
const context: JavaScriptArtifactGraphContext = {
|
||||
...base,
|
||||
fileSet: { ...base.fileSet, files },
|
||||
filesByPath: new Map(files.map((file) => [file.path, file])),
|
||||
};
|
||||
const context = contextFor(analysis);
|
||||
context.applicationRootsBySource.set(setter.path, new Set(["a"]));
|
||||
context.applicationRootsBySource.set(window.path, new Set(["b"]));
|
||||
if (possibleOwner)
|
||||
@@ -387,12 +367,9 @@ it.each([
|
||||
{ file: otherPage, javascript: null, semantic: null },
|
||||
];
|
||||
const analysis = analysisFor(entries);
|
||||
const base = contextFor(main, analysis);
|
||||
const files = entries.map(({ file }) => file);
|
||||
const base = contextFor(analysis);
|
||||
const context: JavaScriptArtifactGraphContext = {
|
||||
...base,
|
||||
fileSet: { ...base.fileSet, files },
|
||||
filesByPath: new Map(files.map((file) => [file.path, file])),
|
||||
unanalyzedFiles:
|
||||
gap === "opaque"
|
||||
? {
|
||||
@@ -460,7 +437,7 @@ it("attaches sender validation to the innermost handler after a long minified li
|
||||
const javascript = analyzeJavaScriptStaticSource(source);
|
||||
expect(javascript.electron.sender_validations).toHaveLength(1);
|
||||
const analysis = analysisFor([{ file, javascript, semantic: null }]);
|
||||
const context = contextFor(file, analysis);
|
||||
const context = contextFor(analysis);
|
||||
addJavaScriptArtifactFiles(context);
|
||||
addElectronIpcBoundaries(
|
||||
context,
|
||||
@@ -512,7 +489,7 @@ it("projects package parse status from the package observation", () => {
|
||||
},
|
||||
],
|
||||
};
|
||||
const context = contextFor(file, analysis);
|
||||
const context = contextFor(analysis);
|
||||
addJavaScriptArtifactFiles(context);
|
||||
const observation = context.accumulator
|
||||
.nodes()
|
||||
|
||||
Reference in new issue
Block a user