Make Terminal a native feature
- Move the Terminal plugin into src/lib/features/terminal/ on the host Svelte runtime, with xterm 6.0.0 and addon-fit 0.11.0. - Buttons, shortcuts, Help rows, and the close guard follow the Terminal switch. The first use loads the module. - Add Settings → Terminal and the terminal settings section. The first start copies the old runestone.terminal key. - The 16 ANSI colors come from the --rs-ansi-* tokens. - Off asks first when a shell runs in any window (pty_running). - Rust: no pty-data or pty-exit events, an owner check for write, resize, and close, output joined up to 64 KiB or 8 ms, and the sessions of a reloaded page close. - Remove api.pty, the terminal contribution, TerminalSlot, and the plugin loader of the smoke harness. - Check only the tested switches in the Background smoke test. - Update the tests, the menu inventory, the docs, and UI-058.
This commit is contained in:
@@ -3,7 +3,7 @@ import assert from "node:assert/strict";
|
||||
import { ExternalSession, type SessionServices } from "../../../src/lib/plugins/externalSession.ts";
|
||||
import { ContributionRegistry } from "../../../src/lib/plugins/registry.ts";
|
||||
import { ServiceHub } from "../../../src/lib/plugins/services.ts";
|
||||
import type { FilesTransport, PluginApi, PtyTransport } from "../../../src/lib/plugins/sdk.ts";
|
||||
import type { FilesTransport, PluginApi } from "../../../src/lib/plugins/sdk.ts";
|
||||
|
||||
const base: SessionServices = {
|
||||
workspace: { list: async () => [], read: async () => "", write: async () => {} },
|
||||
@@ -17,82 +17,6 @@ async function start(id: string, services: Partial<SessionServices>) {
|
||||
return { api, session };
|
||||
}
|
||||
|
||||
function fakePty() {
|
||||
const calls: unknown[][] = [];
|
||||
const outputs = new Map<string, { data: (bytes: Uint8Array) => void; exit: (code: number | null) => void }>();
|
||||
let release: (() => void) | undefined;
|
||||
const transport: PtyTransport & { hold(): void } = {
|
||||
hold() { let done!: () => void; const wait = new Promise<void>(resolve => { done = resolve; }); release = done; holding = wait; },
|
||||
open: async (request, data, exit) => { calls.push(["open", request]); outputs.set(request.id, { data, exit }); await holding; },
|
||||
write: async (id, data) => { calls.push(["write", id, data]); },
|
||||
resize: async (id, cols, rows) => { calls.push(["resize", id, cols, rows]); },
|
||||
close: async id => { calls.push(["close", id]); },
|
||||
profiles: async () => [{ id: "pwsh", title: "PowerShell 7", kind: "powershell" }],
|
||||
openWindow: async () => { calls.push(["window"]); },
|
||||
dictation: async () => { calls.push(["dictation"]); },
|
||||
};
|
||||
let holding: Promise<void> = Promise.resolve();
|
||||
return { transport, calls, outputs, release: () => release?.() };
|
||||
}
|
||||
|
||||
test("terminal sessions deliver their own output and close when the plugin stops", async () => {
|
||||
const pty = fakePty();
|
||||
const { api, session } = await start("org.test.terminal", { pty: pty.transport });
|
||||
assert.equal(api.pty!.version, 1);
|
||||
assert.deepEqual(await api.pty!.profiles(), [{ id: "pwsh", title: "PowerShell 7", kind: "powershell" }]);
|
||||
const received: string[] = [];
|
||||
const exits: (number | null)[] = [];
|
||||
const first = await api.pty!.open({ cwd: "C:/work", cols: 80, rows: 24, profile: "pwsh", onData: data => received.push(new TextDecoder().decode(data)), onExit: code => exits.push(code) });
|
||||
const second = await api.pty!.open({ cwd: "C:/work", cols: 80, rows: 24, onData: () => {} });
|
||||
assert.notEqual(first.id, second.id);
|
||||
assert.deepEqual(pty.calls[0], ["open", { id: first.id, cwd: "C:/work", cols: 80, rows: 24, profile: "pwsh" }]);
|
||||
pty.outputs.get(first.id)!.data(new TextEncoder().encode("ready"));
|
||||
await first.write("dir\r");
|
||||
await first.resize(100, 30);
|
||||
assert.deepEqual(received, ["ready"]);
|
||||
assert.deepEqual(pty.calls.slice(-2), [["write", first.id, "dir\r"], ["resize", first.id, 100, 30]]);
|
||||
assert.throws(() => first.resize(0, 30), /positive integers/);
|
||||
// An exited session needs no close at shutdown.
|
||||
pty.outputs.get(first.id)!.exit(0);
|
||||
assert.deepEqual(exits, [0]);
|
||||
await session.stop();
|
||||
assert.deepEqual(pty.calls.filter(call => call[0] === "close"), [["close", second.id]]);
|
||||
pty.outputs.get(second.id)!.data(new TextEncoder().encode("late"));
|
||||
assert.deepEqual(received, ["ready"]);
|
||||
assert.throws(() => api.pty!.open({ cwd: "C:/work", cols: 80, rows: 24, onData: () => {} }), /disabled/);
|
||||
assert.throws(() => api.pty!.openWindow(), /disabled/);
|
||||
assert.throws(() => second.write("x"), /disabled/);
|
||||
// Close stays safe after shutdown and is not repeated.
|
||||
await second.close();
|
||||
assert.equal(pty.calls.filter(call => call[0] === "close").length, 1);
|
||||
});
|
||||
|
||||
test("a terminal session that starts after shutdown is closed", async () => {
|
||||
const pty = fakePty();
|
||||
const { api, session } = await start("org.test.terminal", { pty: pty.transport });
|
||||
pty.transport.hold();
|
||||
const opening = api.pty!.open({ cwd: "C:/work", cols: 80, rows: 24, onData: () => {} });
|
||||
const stopping = session.stop();
|
||||
pty.release();
|
||||
await assert.rejects(opening, /disabled/);
|
||||
await stopping;
|
||||
const id = (pty.calls[0][1] as { id: string }).id;
|
||||
assert.deepEqual(pty.calls.filter(call => call[0] === "close"), [["close", id]]);
|
||||
});
|
||||
|
||||
test("a closed terminal session rejects input and closes once", async () => {
|
||||
const pty = fakePty();
|
||||
const { api } = await start("org.test.terminal", { pty: pty.transport });
|
||||
const output: string[] = [];
|
||||
const terminal = await api.pty!.open({ cwd: "C:/work", cols: 80, rows: 24, onData: data => output.push(String(data.length)) });
|
||||
await Promise.all([terminal.close(), terminal.close()]);
|
||||
assert.equal(pty.calls.filter(call => call[0] === "close").length, 1);
|
||||
assert.throws(() => terminal.write("x"), /closed/);
|
||||
pty.outputs.get(terminal.id)!.data(new Uint8Array([1]));
|
||||
assert.deepEqual(output, []);
|
||||
await assert.rejects(Promise.resolve().then(() => api.pty!.open({ cwd: "C:/work", cols: 80, rows: 24 } as never)), /onData/);
|
||||
});
|
||||
|
||||
test("folder grant calls check shutdown before and after the native request", async () => {
|
||||
const calls: unknown[][] = [];
|
||||
let finish: ((value: unknown) => void) | undefined;
|
||||
|
||||
@@ -5,9 +5,9 @@ import { ExternalSession } from "../../../src/lib/plugins/externalSession.ts";
|
||||
import type { PluginApi } from "../../../src/lib/plugins/sdk.ts";
|
||||
import type { Contributions } from "../../../src/lib/plugins/contracts.ts";
|
||||
|
||||
// A small test-only package. It contributes a feature, as the optional plugins do.
|
||||
const terminal: Contributions["terminal"] = { mount: () => ({ update() {}, command() {}, dispose() {} }), shortcut: () => null };
|
||||
const activate = (api: PluginApi) => { api.contribute("terminal", terminal); };
|
||||
// A small test-only package. It contributes the browser, as the built-in browser module does.
|
||||
const browser: Contributions["browser"] = { mountSettings: () => () => {}, mountBookmarks: () => () => {}, open: async () => false };
|
||||
const activate = (api: PluginApi) => { api.contribute("browser", browser); };
|
||||
|
||||
const services = {
|
||||
workspace: { list: async () => [], read: async () => "", write: async () => {} },
|
||||
@@ -37,23 +37,24 @@ test("optional workspace creation and reads stop when the plugin is disabled", a
|
||||
assert.equal(files.size, 1);
|
||||
});
|
||||
test("core starts without optional features and package contributions end with their session", async () => {
|
||||
assert.equal(pluginHost.get("terminal"), undefined);
|
||||
assert.equal(pluginHost.get("browser"), undefined);
|
||||
const registry = new ContributionRegistry();
|
||||
const session = new ExternalSession("org.test.feature", services, () => {}, () => {}, registry);
|
||||
await session.start({ activate });
|
||||
assert.equal(registry.get("terminal"), terminal);
|
||||
assert.equal(registry.get("browser"), browser);
|
||||
await session.stop();
|
||||
assert.equal(registry.get("terminal"), undefined);
|
||||
assert.equal(registry.get("browser"), undefined);
|
||||
const next = new ExternalSession("org.test.feature", services, () => {}, () => {}, registry);
|
||||
await next.start({ activate });
|
||||
assert.equal(registry.get("terminal"), terminal);
|
||||
assert.equal(registry.get("browser"), browser);
|
||||
await next.stop();
|
||||
assert.equal(registry.get("terminal"), undefined);
|
||||
assert.equal(registry.get("browser"), undefined);
|
||||
});
|
||||
|
||||
test("unknown contribution keys are rejected", () => {
|
||||
const registry = new ContributionRegistry();
|
||||
for (const key of ["tableFormulas", "git"]) assert.throws(() => registry.register("old", key as keyof Contributions, terminal as never), /Unknown plugin contribution/);
|
||||
// `terminal` was a contribution until the Terminal feature became native.
|
||||
for (const key of ["tableFormulas", "git", "terminal"]) assert.throws(() => registry.register("old", key as keyof Contributions, browser as never), /Unknown plugin contribution/);
|
||||
});
|
||||
|
||||
test("package contribution conflicts preserve the first owner and revoke access before cleanup", async () => {
|
||||
@@ -62,13 +63,13 @@ test("package contribution conflicts preserve the first owner and revoke access
|
||||
const second = new ExternalSession("second", services, () => {}, () => {}, registry);
|
||||
let api!: PluginApi;
|
||||
await first.start({ activate(value) { api = value; activate(api); } });
|
||||
const contribution = registry.get("terminal");
|
||||
const contribution = registry.get("browser");
|
||||
await assert.rejects(second.start({ activate }), /already registered/);
|
||||
await second.stop();
|
||||
assert.equal(registry.get("terminal"), contribution);
|
||||
assert.equal(registry.get("browser"), contribution);
|
||||
api.onDispose(() => {
|
||||
assert.equal(registry.get("terminal"), undefined);
|
||||
assert.throws(() => api.contribute("terminal", contribution!), /disabled/);
|
||||
assert.equal(registry.get("browser"), undefined);
|
||||
assert.throws(() => api.contribute("browser", contribution!), /disabled/);
|
||||
});
|
||||
await first.stop();
|
||||
});
|
||||
@@ -82,5 +83,5 @@ test("partial activation and failed cleanup cannot retain feature contributions"
|
||||
throw new Error("Activation failed");
|
||||
} }), /Activation failed/);
|
||||
await assert.rejects(session.stop(), /Cleanup failed/);
|
||||
assert.equal(registry.get("terminal"), undefined);
|
||||
assert.equal(registry.get("browser"), undefined);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user