188 lines
11 KiB
TypeScript
188 lines
11 KiB
TypeScript
// The main-bundle parts of the native Project Management feature: the catalog entry, the board view pattern of the
|
|
// session, the settings and their first-start migration, and the board reader of Background and Settings.
|
|
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { featureEntry, featureOfOwner } from "../../../../src/lib/features/catalog.ts";
|
|
import { FeatureSession } from "../../../../src/lib/features/session.ts";
|
|
import { migrateFeatures, type LegacyPlugin } from "../../../../src/lib/features/migration.ts";
|
|
import type { FeatureContext, FeatureInstance } from "../../../../src/lib/features/types.ts";
|
|
import { boardOfView, boardViewId, PLANNING } from "../../../../src/lib/features/pm/views.ts";
|
|
import { DEFAULT_PROJECT_MANAGEMENT, migrateProjectManagement, normalizeProjectManagement, readViewStates, VIEW_STATES_KEY, writeViewStates, type ProjectManagementMigrationHost, type ProjectManagementSettings } from "../../../../src/lib/features/pmSettings.ts";
|
|
import { BOARD_CATALOG_PATH, parseBoardCatalog, readBoardCatalog, writeDefaultBoard } from "../../../../src/lib/features/pmBoards.ts";
|
|
import { SETTINGS_SECTIONS, LEGACY_KEYS } from "../../../../src/lib/settings/store.ts";
|
|
import { defaultBoard } from "../../../../src/lib/features/pm/storage/board.ts";
|
|
import { parseCatalog } from "../../../../src/lib/features/pm/storage/catalog.ts";
|
|
|
|
function memoryStorage(initial: Record<string, string> = {}) {
|
|
const values = new Map(Object.entries(initial));
|
|
return { values, getItem: (key: string) => values.get(key) ?? null, setItem: (key: string, value: string) => { values.set(key, value); } };
|
|
}
|
|
|
|
function context(log: string[] = []): FeatureContext {
|
|
return {
|
|
openView: id => { log.push(`open ${id}`); }, openFile: async () => {}, onWorkspaceChange: () => () => {},
|
|
setViewTitle: (id, title) => { log.push(`title ${id} ${title}`); }, closeView: id => { log.push(`close ${id}`); },
|
|
replaceView: (current, next) => { log.push(`replace ${current} ${next}`); },
|
|
};
|
|
}
|
|
|
|
test("Project Management keeps the plugin ID, is on by default, and follows its package", () => {
|
|
const pm = featureEntry("pm");
|
|
assert.equal(pm.owner, "runestone.pm");
|
|
assert.equal(featureOfOwner("runestone.pm")?.id, "pm");
|
|
assert.equal(pm.defaultOn, true);
|
|
assert.equal(pm.followsPackage, true);
|
|
assert.deepEqual(pm.views.map(view => [view.id, view.placement, view.launch]), [["planning", "editor", true]]);
|
|
assert.deepEqual(pm.documentTypes, [], "The file tree reads no Markdown file for Project Management.");
|
|
assert.equal((SETTINGS_SECTIONS as readonly string[]).includes("projectManagement"), true);
|
|
assert.equal(LEGACY_KEYS.projectManagement, "runestone.project-management");
|
|
});
|
|
|
|
test("board view IDs are the plugin IDs, and only valid board IDs match", () => {
|
|
const id = "0c18bcdc-f41e-4a31-87f4-87c369f398c1";
|
|
const view = boardViewId(id);
|
|
assert.equal(view, "board-" + Buffer.from(id).toString("hex"));
|
|
assert.equal(boardOfView(view), id);
|
|
assert.equal(boardOfView(boardViewId("Plan ✓")), "Plan ✓");
|
|
for (const other of [PLANNING, "board-", "board-6", "board-zz", "board-ff", "board-41-42", "graph"]) assert.equal(boardOfView(other), null, other);
|
|
const pattern = featureEntry("pm").viewPatterns![0].match;
|
|
assert.equal(pattern.test(view), true);
|
|
assert.equal(pattern.test(PLANNING), false);
|
|
assert.equal(pattern.test(view + "x"), false);
|
|
});
|
|
|
|
test("the session answers for board views before the module loads, and gives them the board name later", async () => {
|
|
let loads = 0;
|
|
const log: string[] = [];
|
|
const restored: [string, unknown][] = [];
|
|
const mounted: string[] = [];
|
|
const instance: FeatureInstance = {
|
|
mount: view => { mounted.push(view); return () => {}; },
|
|
launch: view => { log.push(`launch ${view}`); },
|
|
restoreTabState: (view, state) => { restored.push([view, state]); },
|
|
saveTabState: view => ({ view }),
|
|
dispose() {},
|
|
};
|
|
const session = new FeatureSession(featureEntry("pm"), context(log), async () => { loads++; return { start: () => instance }; });
|
|
const view = boardViewId("board-a");
|
|
assert.equal(session.views.has(view), false);
|
|
const pattern = session.patternView(view)!;
|
|
assert.equal(pattern.placement, "editor");
|
|
assert.equal(pattern.title, "Project Management");
|
|
assert.equal(pattern.navigation, false);
|
|
assert.equal(pattern.launcher, false);
|
|
assert.equal(session.patternView(view), pattern, "One view for each ID");
|
|
assert.equal(session.patternView("board-zz"), undefined);
|
|
assert.equal(loads, 0, "No module load for a pattern view");
|
|
assert.equal(session.setViewTitle(view, "Roadmap"), true);
|
|
assert.equal(session.setViewTitle(view, "Roadmap"), false);
|
|
assert.equal(session.setViewTitle("planning", "Other"), false, "A catalog view keeps its title");
|
|
assert.equal(pattern.title, "Roadmap");
|
|
// A tab transfer restores the state, which loads the module.
|
|
await pattern.restoreTabState!({ search: "x" });
|
|
assert.deepEqual(restored, [[view, { search: "x" }]]);
|
|
assert.equal(loads, 1);
|
|
assert.deepEqual(pattern.saveTabState!(), { view });
|
|
const element = { replaceChildren() {} } as unknown as HTMLElement;
|
|
const dispose = await session.mount(boardViewId("board-b"), element);
|
|
assert.deepEqual(mounted, [boardViewId("board-b")]);
|
|
await dispose();
|
|
// The ribbon button launches `planning` through the feature, which chooses the default board.
|
|
await session.launch("planning");
|
|
assert.deepEqual(log, ["launch planning"]);
|
|
await session.stop();
|
|
assert.equal(session.patternView(boardViewId("board-c")), undefined, "An off feature makes no views");
|
|
});
|
|
|
|
test("the settings keep 2, 3, or 5 priority levels", () => {
|
|
assert.deepEqual(normalizeProjectManagement(undefined), DEFAULT_PROJECT_MANAGEMENT);
|
|
assert.deepEqual(normalizeProjectManagement({ priorityLevels: 5, other: 1 }), { priorityLevels: 5 });
|
|
assert.deepEqual(normalizeProjectManagement({ priorityLevels: 2 }), { priorityLevels: 2 });
|
|
for (const value of [4, "5", null, 0]) assert.deepEqual(normalizeProjectManagement({ priorityLevels: value }), { priorityLevels: 3 });
|
|
});
|
|
|
|
test("view states stay in localStorage, and a write keeps the states of other tabs", () => {
|
|
const storage = memoryStorage({ [VIEW_STATES_KEY]: JSON.stringify({ planning: { view: "gantt" } }) });
|
|
writeViewStates({ "board-61": { search: "bug" } }, storage);
|
|
assert.deepEqual(readViewStates(storage), { planning: { view: "gantt" }, "board-61": { search: "bug" } });
|
|
assert.deepEqual(readViewStates(memoryStorage({ [VIEW_STATES_KEY]: "not json" })), {});
|
|
});
|
|
|
|
function pmHost(missing: boolean, storage = memoryStorage()) {
|
|
const written: ProjectManagementSettings[] = [];
|
|
const host: ProjectManagementMigrationHost & { written: ProjectManagementSettings[] } = {
|
|
written, missing: () => missing, storage, write: async value => { written.push(value); },
|
|
};
|
|
return host;
|
|
}
|
|
|
|
test("the first start makes the section and the view states from the kept plugin settings", async () => {
|
|
const legacy: LegacyPlugin = { id: "runestone.pm", installed: true, enabled: true, settings: { priorityLevels: 5, viewStates: { planning: { view: "calendar" } }, other: true } };
|
|
const host = pmHost(true);
|
|
assert.deepEqual(await migrateProjectManagement(legacy, host), { priorityLevels: 5 });
|
|
assert.deepEqual(host.written, [{ priorityLevels: 5 }]);
|
|
assert.deepEqual(JSON.parse(host.storage.getItem(VIEW_STATES_KEY)!), { planning: { view: "calendar" } });
|
|
// Existing view states in localStorage stay.
|
|
const kept = pmHost(true, memoryStorage({ [VIEW_STATES_KEY]: "{\"planning\":{}}" }));
|
|
await migrateProjectManagement(legacy, kept);
|
|
assert.equal(kept.storage.getItem(VIEW_STATES_KEY), "{\"planning\":{}}");
|
|
// Without plugin settings, the defaults apply. A present section stays.
|
|
const fresh = pmHost(true);
|
|
assert.deepEqual(await migrateProjectManagement(undefined, fresh), DEFAULT_PROJECT_MANAGEMENT);
|
|
assert.equal(fresh.storage.getItem(VIEW_STATES_KEY), null);
|
|
const present = pmHost(false);
|
|
assert.equal(await migrateProjectManagement(legacy, present), null);
|
|
assert.deepEqual(present.written, []);
|
|
});
|
|
|
|
test("the feature migration asks for the Project Management package when the section is missing", async () => {
|
|
const asked: string[][] = [];
|
|
const switches: unknown[] = [];
|
|
const host = { read: () => ({ git: true, background: true, terminal: true, pdf: true }), write: async (value: unknown) => { switches.push(value); },
|
|
legacy: async (ids: string[]) => { asked.push(ids); return ids.map(id => ({ id, installed: true, enabled: false, settings: { priorityLevels: 2 } })); } };
|
|
const background = { missing: () => false, hasImage: () => false, migrateFolder: async () => false, write: async () => {} };
|
|
const terminal = { missing: () => false, legacy: () => null, write: async () => {} };
|
|
const pm = pmHost(true);
|
|
assert.deepEqual(await migrateFeatures(host, background, terminal, pm), ["pm"]);
|
|
assert.deepEqual(asked, [["runestone.pm"]]);
|
|
assert.deepEqual(switches, [{ git: true, background: true, terminal: true, pdf: true, pm: false }], "The switch follows the disabled package.");
|
|
assert.deepEqual(pm.written, [{ priorityLevels: 2 }]);
|
|
});
|
|
|
|
test("the board reader lists the boards of workspace.json without the feature code", async () => {
|
|
const value = {
|
|
schema: 1, defaultBoardId: "b", defaultBoardPath: "pm-board:b", future: { keep: true },
|
|
boards: [{ name: "Roadmap", value: defaultBoard("a") }, { name: "Bugs", value: defaultBoard("b"), archived: true }, { name: "Copy", value: defaultBoard("a") }, { name: "No value" }],
|
|
};
|
|
const source = JSON.stringify(value);
|
|
assert.deepEqual(parseBoardCatalog(source), { source, defaultBoardId: "b", boards: [{ id: "a", name: "Roadmap", archived: false }, { id: "b", name: "Bugs", archived: true }] });
|
|
assert.deepEqual(parseBoardCatalog(null), { boards: [], defaultBoardId: null, source: null });
|
|
assert.throws(() => parseBoardCatalog("{\"schema\":2,\"boards\":[]}"), /unsupported/);
|
|
const reads: string[] = [];
|
|
const catalog = await readBoardCatalog(async path => { reads.push(path); return source; });
|
|
assert.deepEqual(reads, [BOARD_CATALOG_PATH]);
|
|
assert.deepEqual(catalog.boards.map(board => board.id), ["a", "b"]);
|
|
});
|
|
|
|
test("Settings saves the default board with a guarded write and keeps the other keys", async () => {
|
|
const files = new Map([[BOARD_CATALOG_PATH, JSON.stringify({ schema: 1, boards: [{ name: "Roadmap", value: defaultBoard("a") }], future: 1 }, null, 2) + "\n"]]);
|
|
const read = async (path: string) => files.get(path) ?? null;
|
|
const write = async (path: string, contents: string, expected: string) => {
|
|
if (files.get(path) !== expected) throw new Error("The file changed.");
|
|
files.set(path, contents);
|
|
};
|
|
await writeDefaultBoard("a", read, write);
|
|
const saved = JSON.parse(files.get(BOARD_CATALOG_PATH)!);
|
|
assert.equal(saved.defaultBoardId, "a");
|
|
assert.equal(saved.defaultBoardPath, "pm-board:a");
|
|
assert.equal(saved.future, 1);
|
|
// The feature code accepts the file.
|
|
assert.equal(parseCatalog(files.get(BOARD_CATALOG_PATH)!).defaultBoardId, "a");
|
|
await writeDefaultBoard(null, read, write);
|
|
assert.equal(JSON.parse(files.get(BOARD_CATALOG_PATH)!).defaultBoardId, null);
|
|
await assert.rejects(writeDefaultBoard("missing", read, write), /unavailable/);
|
|
await assert.rejects(writeDefaultBoard("a", read, async () => { throw new Error("The file changed."); }), /changed/);
|
|
// Without a catalog, All tasks needs no write.
|
|
await writeDefaultBoard(null, async () => null, async () => { throw new Error("no write"); });
|
|
});
|