mirror of
https://github.com/AikidoSec/safe-chain.git
synced 2026-05-26 20:20:49 +00:00
Fix tests to match new behavior
This commit is contained in:
parent
240123372a
commit
8950d528d5
1 changed files with 10 additions and 41 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
import assert from "node:assert/strict";
|
import assert from "node:assert/strict";
|
||||||
import { describe, it, mock } from "node:test";
|
import { beforeEach, describe, it, mock } from "node:test";
|
||||||
import { setTimeout } from "node:timers/promises";
|
import { setTimeout } from "node:timers/promises";
|
||||||
import {
|
import {
|
||||||
MALWARE_ACTION_PROMPT,
|
MALWARE_ACTION_PROMPT,
|
||||||
|
|
@ -88,6 +88,11 @@ describe("scanCommand", async () => {
|
||||||
|
|
||||||
const { scanCommand } = await import("./index.js");
|
const { scanCommand } = await import("./index.js");
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
// Reset malware action back to prompt mode for other tests
|
||||||
|
malwareAction = MALWARE_ACTION_PROMPT;
|
||||||
|
});
|
||||||
|
|
||||||
it("should succeed when there are no changes", async () => {
|
it("should succeed when there are no changes", async () => {
|
||||||
let progressWasStopped = false;
|
let progressWasStopped = false;
|
||||||
mockStartProcess.mock.mockImplementationOnce(() => ({
|
mockStartProcess.mock.mockImplementationOnce(() => ({
|
||||||
|
|
@ -205,7 +210,6 @@ describe("scanCommand", async () => {
|
||||||
mockConfirm.mock.resetCalls();
|
mockConfirm.mock.resetCalls();
|
||||||
|
|
||||||
let failureMessageWasSet = false;
|
let failureMessageWasSet = false;
|
||||||
let exitCode = null;
|
|
||||||
|
|
||||||
mockStartProcess.mock.mockImplementationOnce(() => ({
|
mockStartProcess.mock.mockImplementationOnce(() => ({
|
||||||
setText: () => {},
|
setText: () => {},
|
||||||
|
|
@ -220,27 +224,10 @@ describe("scanCommand", async () => {
|
||||||
{ name: "malicious", version: "1.0.0" },
|
{ name: "malicious", version: "1.0.0" },
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// Mock process.exit
|
const result = await scanCommand(["install", "malicious"]);
|
||||||
const originalExit = process.exit;
|
|
||||||
process.exit = mock.fn((code) => {
|
|
||||||
exitCode = code;
|
|
||||||
throw new Error("Process exit called"); // Prevent actual exit
|
|
||||||
});
|
|
||||||
|
|
||||||
try {
|
|
||||||
await assert.rejects(
|
|
||||||
scanCommand(["install", "malicious"]),
|
|
||||||
/Process exit called/
|
|
||||||
);
|
|
||||||
} finally {
|
|
||||||
// Restore original process.exit
|
|
||||||
process.exit = originalExit;
|
|
||||||
// Reset malware action back to prompt mode for other tests
|
|
||||||
malwareAction = MALWARE_ACTION_PROMPT;
|
|
||||||
}
|
|
||||||
|
|
||||||
assert.equal(failureMessageWasSet, true);
|
assert.equal(failureMessageWasSet, true);
|
||||||
assert.equal(exitCode, 1);
|
assert.equal(result, 1);
|
||||||
// Confirm should not have been called in block mode
|
// Confirm should not have been called in block mode
|
||||||
assert.equal(mockConfirm.mock.callCount(), 0);
|
assert.equal(mockConfirm.mock.callCount(), 0);
|
||||||
});
|
});
|
||||||
|
|
@ -252,7 +239,6 @@ describe("scanCommand", async () => {
|
||||||
// Reset mock call count
|
// Reset mock call count
|
||||||
mockConfirm.mock.resetCalls();
|
mockConfirm.mock.resetCalls();
|
||||||
|
|
||||||
let processExited = false;
|
|
||||||
let userWasPrompted = false;
|
let userWasPrompted = false;
|
||||||
|
|
||||||
mockStartProcess.mock.mockImplementationOnce(() => ({
|
mockStartProcess.mock.mockImplementationOnce(() => ({
|
||||||
|
|
@ -271,26 +257,9 @@ describe("scanCommand", async () => {
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
|
|
||||||
// Mock process.exit
|
const result = await scanCommand(["install", "malicious"]);
|
||||||
const originalExit = process.exit;
|
|
||||||
process.exit = mock.fn(() => {
|
|
||||||
processExited = true;
|
|
||||||
throw new Error("Process exit called"); // Prevent actual exit
|
|
||||||
});
|
|
||||||
|
|
||||||
try {
|
assert.equal(result, 1);
|
||||||
await assert.rejects(
|
|
||||||
scanCommand(["install", "malicious"]),
|
|
||||||
/Process exit called/
|
|
||||||
);
|
|
||||||
} finally {
|
|
||||||
// Restore original process.exit
|
|
||||||
process.exit = originalExit;
|
|
||||||
// Reset malware action back to prompt mode for other tests
|
|
||||||
malwareAction = MALWARE_ACTION_PROMPT;
|
|
||||||
}
|
|
||||||
|
|
||||||
assert.equal(processExited, true);
|
|
||||||
assert.equal(userWasPrompted, false);
|
assert.equal(userWasPrompted, false);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue