fix: pr feedbacks

This commit is contained in:
Nicolas Meienberger 2026-04-13 23:12:51 +02:00
parent d5eea2fb97
commit 4a73184a5a
No known key found for this signature in database
6 changed files with 53 additions and 5 deletions

View file

@ -1,3 +1,4 @@
ZEROBYTE_DATABASE_URL=:memory:
APP_SECRET=8b9acd4456dd5db0a4a3c4f4e1240b2c3ae08bb59690167197425e4a25dd9a69
BASE_URL=http://localhost:4096
ENABLE_LOCAL_AGENT=true

View file

@ -1,6 +1,7 @@
import { EventEmitter } from "node:events";
import { PassThrough } from "node:stream";
import { afterEach, expect, test, vi } from "vitest";
import { afterEach, beforeEach, expect, test, vi } from "vitest";
import type { ProcessWithAgentRuntime } from "../helpers/runtime-state.dev";
const spawnMock = vi.fn();
@ -8,7 +9,19 @@ vi.mock("node:child_process", async () => {
return { spawn: spawnMock };
});
const { spawnLocalAgent, stopLocalAgent } = await import("../agents-manager");
let spawnLocalAgent: (typeof import("../agents-manager"))["spawnLocalAgent"];
let stopLocalAgent: (typeof import("../agents-manager"))["stopLocalAgent"];
const processWithAgentRuntime = process as ProcessWithAgentRuntime;
const setAgentRuntime = () => {
processWithAgentRuntime.__zerobyteAgentRuntime = {
agentManager: null,
localAgent: null,
isStoppingLocalAgent: false,
localAgentRestartTimeout: null,
};
};
type FakeChildProcess = EventEmitter & {
stdout: PassThrough;
@ -34,8 +47,15 @@ const createFakeChild = () => {
return child;
};
beforeEach(async () => {
vi.resetModules();
setAgentRuntime();
({ spawnLocalAgent, stopLocalAgent } = await import("../agents-manager"));
});
afterEach(async () => {
await stopLocalAgent();
delete processWithAgentRuntime.__zerobyteAgentRuntime;
spawnMock.mockReset();
vi.restoreAllMocks();
vi.useRealTimers();

View file

@ -152,6 +152,7 @@ export const startAgentRuntime = async () => {
if (runtime.agentManager) {
await runtime.agentManager.stop();
runtime.agentManager = null;
}
const { createAgentManagerRuntime } = await import("./controller/server");

View file

@ -147,8 +147,8 @@ export const createControllerAgentSession = (
yield* Effect.sync(() => {
try {
const sendResult = socket.send(message);
if (sendResult <= 0) {
handleSendFailure(sendResult === 0 ? "connection issue" : "backpressure");
if (sendResult === 0) {
handleSendFailure("connection issue");
}
} catch (error) {
handleSendFailure(toMessage(error));

View file

@ -283,6 +283,29 @@ describe("stop backup", () => {
);
});
test("should settle and mark the backup as failed when the backup process throws", async () => {
const { resticBackupMock } = setup();
const volume = await createTestVolume();
const repository = await createTestRepository();
const schedule = await createTestBackupSchedule({
volumeId: volume.id,
repositoryId: repository.id,
});
resticBackupMock.mockImplementationOnce(() => Promise.reject(new Error("restic crashed")));
const result = await Promise.race([
backupsService.executeBackup(schedule.id).then(() => "settled"),
new Promise<string>((resolve) => setTimeout(() => resolve("timed-out"), 100)),
]);
expect(result).toBe("settled");
const updatedSchedule = await getScheduleByIdOrShortId(schedule.id);
expect(updatedSchedule.lastBackupStatus).toBe("error");
expect(updatedSchedule.lastBackupError).toBe("Error: restic crashed");
});
test("should block forget on the same repository until the active backup completes", async () => {
const { resticBackupMock, resticForgetMock, runBackupMock } = setup();
const volume = await createTestVolume();

View file

@ -74,7 +74,10 @@ export const createAgentBackupMocks = (
status: "failed",
error: stderrLines.join("\n") || resultWithStderr.stderr || result.error,
});
})().catch(() => {});
})().catch((err) => {
runningBackups.delete(request.scheduleId);
resolve({ status: "failed", error: String(err) });
});
});
},
);