mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-24 16:04:21 +00:00
A file with the read-only attribute set (often kept when copying files in Explorer) opened read-only, and typing into it did nothing beyond the banner. Typing, pasting or dropping into such a file now asks to make it writable (the same question as Toggle Read-only Attribute) and clears the attribute on confirm. Binary, too-large and library documents still refuse edits without asking. Fixes #3479
70 lines
2.7 KiB
TypeScript
70 lines
2.7 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
import type { DocModel } from "./model/document";
|
|
import { offerMakeWritable, readOnlyQuestion, toggleReadOnly } from "./fileAttributes";
|
|
|
|
function model(readOnly: boolean, extra: Partial<{ virtual: boolean; banner: string | null }> = {}) {
|
|
const meta = { readOnly, banner: extra.banner ?? (readOnly ? "readOnly" : null) };
|
|
return {
|
|
path: "/p/a.txt",
|
|
virtual: extra.virtual ?? false,
|
|
getMeta: () => meta,
|
|
setReadOnly: vi.fn((ro: boolean) => void (meta.readOnly = ro)),
|
|
} as unknown as DocModel & { setReadOnly: ReturnType<typeof vi.fn> };
|
|
}
|
|
|
|
let answer = true;
|
|
const { confirm } = vi.hoisted(() => ({ confirm: vi.fn(async () => answer) }));
|
|
vi.mock("@/workbench/messages", () => ({ confirm }));
|
|
|
|
beforeEach(() => confirm.mockClear());
|
|
|
|
describe("Toggle Read-only Attribute", () => {
|
|
it("asks before making a read-only file writable, then toggles", async () => {
|
|
const m = model(true);
|
|
answer = true;
|
|
toggleReadOnly(m);
|
|
expect(confirm).toHaveBeenCalledWith(expect.objectContaining({ title: "Make File Writable", ok: "Make Writable" }));
|
|
await vi.waitFor(() => expect(m.setReadOnly).toHaveBeenCalledWith(false));
|
|
});
|
|
|
|
it("Cancel leaves the file alone", async () => {
|
|
const m = model(false);
|
|
answer = false;
|
|
toggleReadOnly(m);
|
|
expect(confirm).toHaveBeenCalledWith(expect.objectContaining({ title: "Make File Read-only" }));
|
|
await Promise.resolve();
|
|
expect(m.setReadOnly).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("does nothing for library and binary documents", () => {
|
|
toggleReadOnly(model(true, { virtual: true }));
|
|
toggleReadOnly(model(true, { banner: "binary" }));
|
|
expect(confirm).not.toHaveBeenCalled();
|
|
expect(readOnlyQuestion("a.txt", false).confirm).toBe("Make Read-only");
|
|
});
|
|
});
|
|
|
|
describe("editing a read-only file", () => {
|
|
it("offers to make it writable once, then lets edits through", async () => {
|
|
const m = model(true);
|
|
answer = true;
|
|
expect(offerMakeWritable(m)).toBe(true);
|
|
expect(offerMakeWritable(m)).toBe(true);
|
|
expect(confirm).toHaveBeenCalledTimes(1);
|
|
expect(confirm).toHaveBeenCalledWith(expect.objectContaining({ title: "Make File Writable" }));
|
|
await vi.waitFor(() => expect(m.setReadOnly).toHaveBeenCalledWith(false));
|
|
expect(offerMakeWritable(m)).toBe(false);
|
|
});
|
|
|
|
it("refuses edits without asking for library, binary and too-large documents", () => {
|
|
for (const m of [
|
|
model(true, { virtual: true }),
|
|
model(true, { banner: "binary" }),
|
|
model(true, { banner: "tooLarge" }),
|
|
])
|
|
expect(offerMakeWritable(m)).toBe(true);
|
|
expect(confirm).not.toHaveBeenCalled();
|
|
expect(offerMakeWritable(model(false))).toBe(false);
|
|
});
|
|
});
|