Порівняти коміти

...
Автор SHA1 Повідомлення Дата
l0ng-ai a728add5ef fix(inspection): rerun the original inspection scope 2026-11-18 19:34:51 +08:00
2 змінених файлів з 122 додано та 11 видалено

No files matched your search

+92 -5
Переглянути файл
@@ -1,9 +1,24 @@
import { afterEach, expect, it, vi } from "vitest";
import { afterEach, beforeEach, expect, it, vi } from "vitest";
import type { Diagnostic, InspectEvent } from "@/platform/ipc";
import { useProject } from "@/platform/project";
import { installInspectionProfileSync, resetInspection, runInspection, useInspection } from "./inspect";
import {
installInspectionProfileSync,
rerunInspection,
resetInspection,
runInspection,
useInspection,
} from "./inspect";
const mock = vi.hoisted(() => ({ channel: null as null | { onmessage: (event: InspectEvent) => void } }));
const mock = vi.hoisted(() => ({
channel: null as null | { onmessage: (event: InspectEvent) => void },
activeEditor: vi.fn(),
inspect: vi.fn(),
vcsStatus: vi.fn(),
}));
vi.mock("@/editor/api", async (original) => ({
...(await original<typeof import("@/editor/api")>()),
activeEditor: mock.activeEditor,
}));
vi.mock("@tauri-apps/api/core", () => ({
Channel: class {
onmessage = () => {};
@@ -14,11 +29,12 @@ vi.mock("@/platform/ipc", async (original) => ({
isTauri: () => true,
call: (value: unknown) => value,
commands: {
inspectCode: (_id: unknown, _scope: unknown, channel: typeof mock.channel) => {
inspectCode: (_id: unknown, scope: unknown, channel: typeof mock.channel) => {
mock.channel = channel;
return Promise.resolve();
return mock.inspect(scope);
},
cancelRequest: () => Promise.resolve(),
vcsStatus: () => mock.vcsStatus(),
},
}));
vi.mock("@/workbench/contributions", () => ({
@@ -45,11 +61,82 @@ function file(path = "/p/a.ts") {
function done() {
mock.channel!.onmessage({ type: "done", summary: { problems: 1 } } as InspectEvent);
}
beforeEach(() => {
mock.channel = null;
mock.activeEditor.mockReset();
mock.inspect.mockReset().mockResolvedValue(undefined);
mock.vcsStatus.mockReset();
useProject.setState({ project: { root: "/p", name: "p", trusted: true, isGit: true, settings: {} }, settings: {} });
});
afterEach(() => {
resetInspection();
useProject.setState({ project: null, settings: null });
vi.useRealTimers();
});
it("reruns the inspected file and profile while a fresh inspection follows the editor", async () => {
const choice = { kind: "file", directory: "/p" } as const;
mock.activeEditor.mockReturnValue({ ctx: { path: "/p/a.ts" } });
await runInspection(choice, "project");
file();
done();
mock.activeEditor.mockReturnValue({ ctx: { path: "/p/b.ts" } });
rerunInspection();
await vi.waitFor(() => expect(mock.inspect).toHaveBeenCalledTimes(2));
expect(mock.inspect).toHaveBeenNthCalledWith(2, { kind: "files", paths: ["/p/a.ts"] });
expect(useInspection.getState()).toMatchObject({ scopeLabel: "File a.ts", profile: "project" });
done();
mock.activeEditor.mockReturnValue(null);
rerunInspection();
await vi.waitFor(() => expect(mock.inspect).toHaveBeenCalledTimes(3));
expect(mock.inspect).toHaveBeenNthCalledWith(3, { kind: "files", paths: ["/p/a.ts"] });
mock.activeEditor.mockReturnValue({ ctx: { path: "/p/b.ts" } });
await runInspection(choice);
expect(mock.inspect).toHaveBeenNthCalledWith(4, { kind: "files", paths: ["/p/b.ts"] });
expect(useInspection.getState()).toMatchObject({ scopeLabel: "File b.ts", profile: "default" });
});
it("keeps the initial file list and label when rerunning an uncommitted-files inspection", async () => {
mock.vcsStatus.mockResolvedValue([{ path: "/p/a.ts" }, { path: "/p/b.ts" }]);
await runInspection({ kind: "changed", directory: "/p" });
done();
mock.vcsStatus.mockResolvedValue([{ path: "/p/c.ts" }]);
rerunInspection();
await vi.waitFor(() => expect(mock.inspect).toHaveBeenCalledTimes(2));
expect(mock.inspect).toHaveBeenNthCalledWith(2, { kind: "files", paths: ["/p/a.ts", "/p/b.ts"] });
expect(mock.vcsStatus).toHaveBeenCalledTimes(1);
expect(useInspection.getState().scopeLabel).toBe("2 uncommitted files");
});
it("keeps the prior target when it disappears and reports the inspection failure", async () => {
mock.activeEditor.mockReturnValue({ ctx: { path: "/p/a.ts" } });
await runInspection({ kind: "file", directory: "/p" });
done();
mock.activeEditor.mockReturnValue({ ctx: { path: "/p/b.ts" } });
mock.inspect.mockRejectedValueOnce(new Error("File a.ts does not exist"));
rerunInspection();
await vi.waitFor(() => expect(useInspection.getState().error).toBe("File a.ts does not exist"));
expect(mock.inspect).toHaveBeenNthCalledWith(2, { kind: "files", paths: ["/p/a.ts"] });
expect(useInspection.getState().scopeLabel).toBe("File a.ts");
});
it("ignores scopes resolved after their project closed and never reruns across projects", async () => {
await runInspection({ kind: "project", directory: "/p" });
done();
let finishStatus!: (files: { path: string }[]) => void;
mock.vcsStatus.mockReturnValue(new Promise((resolve) => (finishStatus = resolve)));
const pending = runInspection({ kind: "changed", directory: "/p" });
useProject.setState({ project: { root: "/other", name: "other", trusted: true, isGit: true, settings: {} } });
rerunInspection();
finishStatus([{ path: "/p/a.ts" }]);
await pending;
expect(mock.inspect).toHaveBeenCalledTimes(1);
});
it("reprojects completed results, counts and original severities without rerunning", async () => {
const dispose = installInspectionProfileSync();
try {
+30 -6
Переглянути файл
@@ -13,6 +13,7 @@ import {
type InspectEvent,
type InspectSummary,
type RequestId,
type SearchScope,
} from "@/platform/ipc";
import {
activeProfileId,
@@ -97,6 +98,12 @@ export function groupInspection(files: Iterable<InspectedFileResult>): Inspectio
});
}
interface InspectionTarget {
root: string;
scope: SearchScope;
label: string;
}
interface InspectState {
running: boolean;
total: number;
@@ -108,8 +115,8 @@ interface InspectState {
summary: InspectSummary | null;
scopeLabel: string | null;
error: string | null;
/** Scope of the last run (Rerun). */
last: ScopeChoice | null;
/** Resolved target of the last run (Rerun). */
last: InspectionTarget | null;
/** Inspection profile of the last run. */
profile: ProfileId | null;
/** Problems excluded from the results (⌫), by `problemId`. */
@@ -168,6 +175,7 @@ export function installInspectionProfileSync() {
}
let inflight: RequestId | null = null;
let startSeq = 0;
export function cancelInspection() {
if (inflight === null) return;
@@ -176,18 +184,32 @@ export function cancelInspection() {
/** The window switched projects: stops the run and forgets its results. */
export function resetInspection() {
++startSeq;
cancelInspection();
inflight = null;
useInspection.setState(initialInspection());
}
function inspectionRoot(): string | undefined {
const { project, opening } = useProject.getState();
return opening ? undefined : project?.root;
}
/** Inspect Code over `choice` with `profileId` (default: the profile in effect). */
export async function runInspection(choice: ScopeChoice, profileId: ProfileId = activeProfileId()): Promise<void> {
const root = inspectionRoot();
if (!root) return;
const seq = ++startSeq;
const scope = await resolveScope(choice);
if (seq !== startSeq || inspectionRoot() !== root) return;
if (typeof scope === "string") {
notifications().notify({ group: "analysis", level: "warning", title: "Inspect Code", body: scope });
return;
}
await inspectTarget({ root, scope, label: describeScope(choice, scope) }, profileId);
}
async function inspectTarget(target: InspectionTarget, profileId: ProfileId): Promise<void> {
cancelInspection();
const id = nextRequestId();
inflight = id;
@@ -199,8 +221,8 @@ export async function runInspection(choice: ScopeChoice, profileId: ProfileId =
files: new Map(),
summary: null,
error: null,
scopeLabel: describeScope(choice, scope),
last: choice,
scopeLabel: target.label,
last: target,
profile: profileId,
excluded: new Set(),
});
@@ -246,7 +268,7 @@ export async function runInspection(choice: ScopeChoice, profileId: ProfileId =
}
};
try {
await call(commands.inspectCode(id, scope, channel));
await call(commands.inspectCode(id, target.scope, channel));
} catch (e) {
if (inflight === id) {
inflight = null;
@@ -257,5 +279,7 @@ export async function runInspection(choice: ScopeChoice, profileId: ProfileId =
export function rerunInspection() {
const { last, profile } = useInspection.getState();
if (last) void runInspection(last, profile ?? undefined);
if (!last || inspectionRoot() !== last.root) return;
++startSeq;
void inspectTarget(last, profile ?? activeProfileId());
}