Some checks failed
build-packages / resolve bundled mosh-client (push) Has been cancelled
build-packages / resolve bundled et-client (push) Has been cancelled
build-packages / build-macos (push) Has been cancelled
build-packages / build-windows (push) Has been cancelled
build-packages / build-linux-x64 (push) Has been cancelled
build-packages / build-linux-arm64 (push) Has been cancelled
build-packages / release (push) Has been cancelled
build-packages / update Nix release metadata (push) Has been cancelled
build-packages / bump homebrew tap (push) Has been cancelled
test / lint-and-test (push) Has been cancelled
AI automation / Route event (push) Has been cancelled
AI automation / Hand reopened issue to maintainers (push) Has been cancelled
AI automation / Clean source issue state (push) Has been cancelled
AI automation / Reconcile handoffs (push) Has been cancelled
AI automation / Classify issue (push) Has been cancelled
AI automation / Claude Code smoke (push) Has been cancelled
AI automation / Review issue follow-up (push) Has been cancelled
AI automation / Publish issue follow-up (push) Has been cancelled
AI automation / Implement with Claude Code (push) Has been cancelled
AI automation / Publish implement PR (push) Has been cancelled
AI automation / Continue queued issue comments (push) Has been cancelled
AI automation / Codex review loop (push) Has been cancelled
AI automation / Publish Codex fix (push) Has been cancelled
AI automation / Clear Codex dispatch marker (push) Has been cancelled
AI automation / Own PR re-request Codex (push) Has been cancelled
AI automation / External PR re-request Codex (push) Has been cancelled
AI automation / Poll Codex reaction / retry (push) Has been cancelled
build-et-binaries / build-linux-x64 (push) Has been cancelled
build-et-binaries / build-linux-arm64 (push) Has been cancelled
build-et-binaries / build-macos-universal (push) Has been cancelled
build-et-binaries / build-windows-x64 (push) Has been cancelled
build-et-binaries / release (push) Has been cancelled
182 lines
6.0 KiB
TypeScript
182 lines
6.0 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import test from "node:test";
|
|
import React from "react";
|
|
import { act, create, type ReactTestRenderer } from "react-test-renderer";
|
|
import {
|
|
cleanupFailedExternalOpenTemp,
|
|
useExternalFileWatchLifecycle,
|
|
type ExternalFileWatchLifecycle,
|
|
} from "./externalFileWatchLifecycle";
|
|
|
|
test("failed external app launch unregisters and deletes its remote temp file", async () => {
|
|
const calls: string[] = [];
|
|
await cleanupFailedExternalOpenTemp({
|
|
unregisterTempFile: async (sftpId, localPath) => {
|
|
calls.push(`${sftpId}:${localPath}`);
|
|
return { success: true };
|
|
},
|
|
}, "sftp-1", "/tmp/edit.txt");
|
|
assert.deepEqual(calls, ["sftp-1:/tmp/edit.txt"]);
|
|
});
|
|
|
|
test("renderer tracks a reused watch once and releases it on unmount", async () => {
|
|
const stopped: Array<{ watchId: string; cleanupTempFile: boolean }> = [];
|
|
let lifecycle: ExternalFileWatchLifecycle | null = null;
|
|
let renderer: ReactTestRenderer | null = null;
|
|
|
|
function Probe() {
|
|
lifecycle = useExternalFileWatchLifecycle(async (watchId, cleanupTempFile) => {
|
|
stopped.push({ watchId, cleanupTempFile });
|
|
});
|
|
return null;
|
|
}
|
|
|
|
await act(async () => {
|
|
renderer = create(React.createElement(Probe));
|
|
});
|
|
await act(async () => {
|
|
lifecycle!.remember("watch-reused");
|
|
lifecycle!.remember("watch-reused");
|
|
});
|
|
assert.equal(lifecycle!.activeCountRef.current, 1);
|
|
|
|
await act(async () => renderer!.unmount());
|
|
await new Promise((resolve) => setImmediate(resolve));
|
|
|
|
assert.deepEqual(stopped, [{ watchId: "watch-reused", cleanupTempFile: false }]);
|
|
});
|
|
|
|
test("explicit SFTP lifecycle cleanup releases every tracked watch and temp file", async () => {
|
|
const stopped: Array<{ watchId: string; cleanupTempFile: boolean }> = [];
|
|
let lifecycle: ExternalFileWatchLifecycle | null = null;
|
|
let renderer: ReactTestRenderer | null = null;
|
|
|
|
function Probe() {
|
|
lifecycle = useExternalFileWatchLifecycle(async (watchId, cleanupTempFile) => {
|
|
stopped.push({ watchId, cleanupTempFile });
|
|
});
|
|
return null;
|
|
}
|
|
|
|
await act(async () => {
|
|
renderer = create(React.createElement(Probe));
|
|
});
|
|
lifecycle!.remember("watch-a");
|
|
lifecycle!.remember("watch-b");
|
|
|
|
await lifecycle!.releaseAll(true);
|
|
|
|
assert.equal(lifecycle!.activeCountRef.current, 0);
|
|
assert.deepEqual(stopped, [
|
|
{ watchId: "watch-a", cleanupTempFile: true },
|
|
{ watchId: "watch-b", cleanupTempFile: true },
|
|
]);
|
|
await act(async () => renderer!.unmount());
|
|
});
|
|
|
|
test("a watch that starts after unmount is stopped instead of being retained", async () => {
|
|
const stopped: Array<{ watchId: string; cleanupTempFile: boolean }> = [];
|
|
let lifecycle: ExternalFileWatchLifecycle | null = null;
|
|
let renderer: ReactTestRenderer | null = null;
|
|
|
|
function Probe() {
|
|
lifecycle = useExternalFileWatchLifecycle(async (watchId, cleanupTempFile) => {
|
|
stopped.push({ watchId, cleanupTempFile });
|
|
});
|
|
return null;
|
|
}
|
|
|
|
await act(async () => {
|
|
renderer = create(React.createElement(Probe));
|
|
});
|
|
const lateLifecycle = lifecycle!;
|
|
await act(async () => renderer!.unmount());
|
|
|
|
lateLifecycle.remember("watch-started-after-unmount");
|
|
await new Promise((resolve) => setImmediate(resolve));
|
|
|
|
assert.deepEqual(stopped, [{
|
|
watchId: "watch-started-after-unmount",
|
|
cleanupTempFile: false,
|
|
}]);
|
|
assert.equal(lateLifecycle.activeCountRef.current, 0);
|
|
});
|
|
|
|
test("release invalidates pending watch starts while a new generation remains usable", async () => {
|
|
const stopped: Array<{ watchId: string; cleanupTempFile: boolean }> = [];
|
|
let lifecycle: ExternalFileWatchLifecycle | null = null;
|
|
let renderer: ReactTestRenderer | null = null;
|
|
|
|
function Probe() {
|
|
lifecycle = useExternalFileWatchLifecycle(async (watchId, cleanupTempFile) => {
|
|
stopped.push({ watchId, cleanupTempFile });
|
|
});
|
|
return null;
|
|
}
|
|
|
|
await act(async () => {
|
|
renderer = create(React.createElement(Probe));
|
|
});
|
|
const oldGeneration = lifecycle!.captureGeneration();
|
|
await lifecycle!.releaseAll(true);
|
|
|
|
lifecycle!.remember("late-old-watch", oldGeneration);
|
|
const currentGeneration = lifecycle!.captureGeneration();
|
|
lifecycle!.remember("fresh-watch", currentGeneration);
|
|
await new Promise((resolve) => setImmediate(resolve));
|
|
|
|
assert.deepEqual(stopped, [{ watchId: "late-old-watch", cleanupTempFile: true }]);
|
|
assert.equal(lifecycle!.activeCountRef.current, 1);
|
|
|
|
await lifecycle!.releaseAll(false);
|
|
assert.deepEqual(stopped, [
|
|
{ watchId: "late-old-watch", cleanupTempFile: true },
|
|
{ watchId: "fresh-watch", cleanupTempFile: false },
|
|
]);
|
|
await act(async () => renderer!.unmount());
|
|
});
|
|
|
|
test("backend session cleanup removes a stopped watch from renderer ownership", async () => {
|
|
const stopped: Array<{ watchId: string; cleanupTempFile: boolean }> = [];
|
|
let onBackendStopped: ((payload: {
|
|
watchId: string;
|
|
localPath?: string;
|
|
}) => void) | null = null;
|
|
let lifecycle: ExternalFileWatchLifecycle | null = null;
|
|
let renderer: ReactTestRenderer | null = null;
|
|
const forgottenPaths: string[] = [];
|
|
|
|
function Probe() {
|
|
lifecycle = useExternalFileWatchLifecycle(
|
|
async (watchId, cleanupTempFile) => {
|
|
stopped.push({ watchId, cleanupTempFile });
|
|
},
|
|
(callback) => {
|
|
onBackendStopped = callback;
|
|
return () => { onBackendStopped = null; };
|
|
},
|
|
(localPath) => { forgottenPaths.push(localPath); },
|
|
);
|
|
return null;
|
|
}
|
|
|
|
await act(async () => {
|
|
renderer = create(React.createElement(Probe));
|
|
});
|
|
lifecycle!.remember("watch-closed-with-session");
|
|
assert.equal(lifecycle!.activeCountRef.current, 1);
|
|
|
|
await act(async () => {
|
|
onBackendStopped?.({
|
|
watchId: "watch-closed-with-session",
|
|
localPath: "/tmp/externally-deleted.txt",
|
|
});
|
|
});
|
|
assert.equal(lifecycle!.activeCountRef.current, 0);
|
|
assert.deepEqual(forgottenPaths, ["/tmp/externally-deleted.txt"]);
|
|
|
|
await act(async () => renderer!.unmount());
|
|
await new Promise((resolve) => setImmediate(resolve));
|
|
assert.deepEqual(stopped, [], "backend-owned cleanup must not issue a duplicate stop");
|
|
});
|