EditorRegistry.close() guarded its active-changed emit on `_activeId != null`, so closing the LAST buffer (active clears to null) emitted only editor.closed — never the active-changed(id:null) the editor extension listens for to call closeEditor(). editorOpen stayed true and the top split sat orphaned over the Claude pane. Always emit active-changed when the active buffer is removed, including the cleared-to-null case; the slot renderer already collapses correctly once editorOpen flips false. The existing extension test fabricated the null active-changed event, so it passed despite the registry never emitting it — that gap is why the bug shipped. Add a registry test that drives the real close() path, plus a slot_host widget test asserting the split (drag handle) drops out and the primary pane fills the column. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
276 lines
12 KiB
Dart
276 lines
12 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:clide/clide.dart';
|
|
import 'package:clide/src/editor/registry.dart';
|
|
import 'package:clide/src/files/path_safety.dart' show PathOutsideRoot;
|
|
import 'package:test/test.dart';
|
|
|
|
void main() {
|
|
late Directory sandbox;
|
|
late RecordingEventSink sink;
|
|
late EditorRegistry reg;
|
|
|
|
setUp(() async {
|
|
sandbox = await Directory.systemTemp.createTemp('clide-editor-test-');
|
|
await File('${sandbox.path}/README.md').writeAsString('# Hello\n\nbody\n');
|
|
sink = RecordingEventSink();
|
|
reg = EditorRegistry(events: sink, workspaceRoot: sandbox);
|
|
});
|
|
|
|
tearDown(() async {
|
|
await reg.shutdown();
|
|
if (sandbox.existsSync()) sandbox.deleteSync(recursive: true);
|
|
});
|
|
|
|
test('open loads file content + emits editor.opened + active-changed', () async {
|
|
final buf = await reg.open('README.md');
|
|
expect(buf.content, contains('Hello'));
|
|
expect(buf.dirty, isFalse);
|
|
expect(reg.active, same(buf));
|
|
expect(sink.ofKind('editor.opened'), hasLength(1));
|
|
expect(sink.ofKind('editor.active-changed'), hasLength(1));
|
|
});
|
|
|
|
test('opening the same path returns the existing buffer', () async {
|
|
final a = await reg.open('README.md');
|
|
final b = await reg.open('README.md');
|
|
expect(b.id, a.id);
|
|
// Still only one open event — re-open is an activate, not a reload.
|
|
expect(sink.ofKind('editor.opened'), hasLength(1));
|
|
});
|
|
|
|
test('insert at caret appends + advances cursor', () async {
|
|
final buf = await reg.open('README.md');
|
|
// caret at 0
|
|
reg.insert(buf.id, 'PREFIX ');
|
|
expect(buf.content.startsWith('PREFIX '), isTrue);
|
|
expect(buf.selection.isCollapsed, isTrue);
|
|
expect(buf.selection.start, 'PREFIX '.length);
|
|
expect(buf.dirty, isTrue);
|
|
expect(sink.ofKind('editor.edited'), hasLength(1));
|
|
});
|
|
|
|
test('replace-selection swaps selected text + resets cursor', () async {
|
|
final buf = await reg.open('README.md');
|
|
reg.setSelection(buf.id, const Selection(start: 2, end: 7)); // 'Hello'
|
|
reg.replaceSelection(buf.id, 'WORLD');
|
|
expect(buf.content.substring(2, 7), 'WORLD');
|
|
expect(buf.selection, const Selection(start: 7, end: 7));
|
|
});
|
|
|
|
test('set-selection clamps out-of-range offsets', () async {
|
|
final buf = await reg.open('README.md');
|
|
reg.setSelection(buf.id, const Selection(start: -5, end: 99999));
|
|
expect(buf.selection.start, 0);
|
|
expect(buf.selection.end, buf.content.length);
|
|
});
|
|
|
|
test('save writes the content back + clears dirty', () async {
|
|
final buf = await reg.open('README.md');
|
|
reg.insert(buf.id, 'X');
|
|
expect(buf.dirty, isTrue);
|
|
final ok = await reg.save(buf.id);
|
|
expect(ok, isTrue);
|
|
expect(buf.dirty, isFalse);
|
|
final onDisk = await File('${sandbox.path}/README.md').readAsString();
|
|
expect(onDisk.startsWith('X'), isTrue);
|
|
expect(sink.ofKind('editor.saved'), hasLength(1));
|
|
});
|
|
|
|
test('close picks a new active buffer when the active one closes', () async {
|
|
final a = await reg.open('README.md');
|
|
await File('${sandbox.path}/b.txt').writeAsString('two');
|
|
final b = await reg.open('b.txt');
|
|
expect(reg.active, same(b));
|
|
reg.close(b.id);
|
|
expect(reg.active, same(a));
|
|
expect(sink.ofKind('editor.closed'), hasLength(1));
|
|
// Active changed at least twice: a→b (on open), b→a (after close)
|
|
expect(sink.ofKind('editor.active-changed').length, greaterThanOrEqualTo(2));
|
|
});
|
|
|
|
test('closing the last buffer clears active + emits active-changed with id=null (T-459)', () async {
|
|
final buf = await reg.open('README.md');
|
|
expect(reg.active, same(buf));
|
|
sink.events.clear();
|
|
reg.close(buf.id);
|
|
// No buffer left — active clears to null.
|
|
expect(reg.active, isNull);
|
|
// The collapse signal: active-changed must still fire, carrying a null id,
|
|
// so the editor split drops out. The bug (T-459) was this emit being
|
|
// guarded away on the last close, leaving editorOpen stuck true.
|
|
final activeChanged = sink.ofKind('editor.active-changed');
|
|
expect(activeChanged, hasLength(1));
|
|
expect(activeChanged.single.data['id'], isNull);
|
|
expect(activeChanged.single.data['path'], isNull);
|
|
expect(sink.ofKind('editor.closed'), hasLength(1));
|
|
});
|
|
|
|
test('opening a non-existent path creates an empty buffer', () async {
|
|
final buf = await reg.open('NEW.md');
|
|
expect(buf.content, isEmpty);
|
|
expect(buf.dirty, isFalse);
|
|
});
|
|
|
|
test('activate(unknown id) is a no-op; activate(known) flips the active buffer', () async {
|
|
final a = await reg.open('README.md');
|
|
final b = await reg.open('NEW.md');
|
|
// b is currently active.
|
|
expect(reg.active, same(b));
|
|
sink.events.clear();
|
|
reg.activate('does-not-exist'); // no-op, no emit
|
|
expect(sink.ofKind('editor.active-changed'), isEmpty);
|
|
reg.activate(a.id);
|
|
expect(reg.active, same(a));
|
|
expect(sink.ofKind('editor.active-changed'), hasLength(1));
|
|
});
|
|
|
|
test('setContent with explicit selection clamps + emits editor.edited replace', () async {
|
|
final buf = await reg.open('README.md');
|
|
sink.events.clear();
|
|
reg.setContent(buf.id, 'short', selection: const Selection(start: 1, end: 99));
|
|
expect(buf.content, 'short');
|
|
// 99 clamped to content.length (5).
|
|
expect(buf.selection.end, 5);
|
|
expect(buf.dirty, isTrue);
|
|
final emitted = sink.ofKind('editor.edited').single;
|
|
expect(emitted.data['kind'], 'replace');
|
|
expect(emitted.data['length'], 5);
|
|
});
|
|
|
|
test('setContent without selection clamps the existing selection to the new content', () async {
|
|
final buf = await reg.open('README.md');
|
|
reg.setSelection(buf.id, const Selection(start: 6, end: 8));
|
|
expect(buf.selection.start, 6);
|
|
reg.setContent(buf.id, 'XY'); // shorter than the selection's offsets
|
|
expect(buf.content, 'XY');
|
|
expect(buf.selection.start, 2);
|
|
expect(buf.selection.end, 2);
|
|
});
|
|
|
|
test('setContent on a missing id is a silent no-op', () {
|
|
sink.events.clear();
|
|
reg.setContent('no-such-id', 'whatever');
|
|
expect(sink.events, isEmpty);
|
|
});
|
|
|
|
test('contentFromArgs decodes content_b64 when text is absent', () {
|
|
expect(EditorRegistry.contentFromArgs({'text': 'plain'}), 'plain');
|
|
expect(EditorRegistry.contentFromArgs({'content_b64': 'aGVsbG8='}), 'hello');
|
|
expect(EditorRegistry.contentFromArgs(const {}), '');
|
|
});
|
|
|
|
test('Selection hashCode + toString round-trip and serialise', () {
|
|
const s = Selection(start: 3, end: 7);
|
|
expect(s.hashCode, const Selection(start: 3, end: 7).hashCode);
|
|
expect(s.hashCode, isNot(equals(const Selection(start: 3, end: 8).hashCode)));
|
|
expect(s.toString(), 'Selection(3-7)');
|
|
});
|
|
|
|
group('.editorconfig (T-29)', () {
|
|
test('open resolves the settings for the file', () async {
|
|
await File('${sandbox.path}/.editorconfig').writeAsString('root = true\n[*]\nindent_style = space\nindent_size = 2\n');
|
|
final buf = await reg.open('README.md');
|
|
expect(buf.settings.indentStyle, 'space');
|
|
expect(buf.settings.indentSize, 2);
|
|
// Exposed over IPC for the UI.
|
|
expect(buf.toJson()['editorSettings'], {'indent_style': 'space', 'indent_size': 2, 'tab_width': 2});
|
|
});
|
|
|
|
test('save trims trailing whitespace + adds a final newline on disk', () async {
|
|
await File('${sandbox.path}/.editorconfig').writeAsString('root = true\n[*]\ntrim_trailing_whitespace = true\ninsert_final_newline = true\n');
|
|
final buf = await reg.open('README.md');
|
|
reg.setContent(buf.id, 'line one \nline two');
|
|
sink.events.clear();
|
|
|
|
await reg.save(buf.id);
|
|
|
|
final onDisk = await File('${sandbox.path}/README.md').readAsString();
|
|
expect(onDisk, 'line one\nline two\n');
|
|
// The in-memory buffer reconciles to the normalized text...
|
|
expect(buf.content, 'line one\nline two\n');
|
|
expect(buf.dirty, isFalse);
|
|
// ...and the UI is told to reload it.
|
|
expect(sink.ofKind('editor.edited'), hasLength(1));
|
|
expect(sink.ofKind('editor.saved'), hasLength(1));
|
|
});
|
|
|
|
test('save normalizes EOL to the configured style', () async {
|
|
await File('${sandbox.path}/.editorconfig').writeAsString('root = true\n[*]\nend_of_line = crlf\n');
|
|
final buf = await reg.open('README.md');
|
|
reg.setContent(buf.id, 'a\nb\n');
|
|
await reg.save(buf.id);
|
|
expect(await File('${sandbox.path}/README.md').readAsString(), 'a\r\nb\r\n');
|
|
});
|
|
|
|
test('save without an editorconfig writes content verbatim (no extra edit event)', () async {
|
|
final buf = await reg.open('README.md');
|
|
reg.setContent(buf.id, 'kept \nas-is');
|
|
sink.events.clear();
|
|
await reg.save(buf.id);
|
|
expect(await File('${sandbox.path}/README.md').readAsString(), 'kept \nas-is');
|
|
expect(sink.ofKind('editor.edited'), isEmpty); // nothing to reconcile
|
|
expect(sink.ofKind('editor.saved'), hasLength(1));
|
|
});
|
|
|
|
test('saving a .editorconfig re-resolves open buffers and notifies', () async {
|
|
// README opens with no rules in effect.
|
|
final readme = await reg.open('README.md');
|
|
expect(readme.settings.indentSize, isNull);
|
|
|
|
// Author a .editorconfig in the editor and save it.
|
|
final cfg = await reg.open('.editorconfig');
|
|
reg.setContent(cfg.id, 'root = true\n[*]\nindent_size = 4\n');
|
|
sink.events.clear();
|
|
await reg.save(cfg.id);
|
|
|
|
// The open README picks up the new rules without reopening.
|
|
expect(readme.settings.indentSize, 4);
|
|
final changed = sink.ofKind('editor.settings-changed');
|
|
expect(changed.map((e) => e.data['id']), contains(readme.id));
|
|
});
|
|
});
|
|
|
|
// T-363: editor.open/save returned absolute paths verbatim and did no
|
|
// `..` normalization — an unconfined read AND write primitive over IPC
|
|
// while files.read was carefully guarded.
|
|
group('path confinement (T-363)', () {
|
|
test('open rejects .. traversal out of the workspace', () async {
|
|
final outside = await Directory.systemTemp.createTemp('clide-editor-outside-');
|
|
addTearDown(() => outside.deleteSync(recursive: true));
|
|
await File('${outside.path}/secret.txt').writeAsString('secret');
|
|
final escape = '../${outside.path.split('/').last}/secret.txt';
|
|
await expectLater(reg.open(escape), throwsA(isA<PathOutsideRoot>()));
|
|
});
|
|
|
|
test('open rejects absolute paths outside the workspace', () async {
|
|
await expectLater(reg.open('/etc/hostname'), throwsA(isA<PathOutsideRoot>()));
|
|
});
|
|
|
|
test('open accepts an absolute path inside the workspace', () async {
|
|
final buf = await reg.open('${sandbox.path}/README.md');
|
|
expect(buf.content, contains('Hello'));
|
|
});
|
|
|
|
test('open rejects a symlink pointing outside the workspace', () async {
|
|
final outside = await Directory.systemTemp.createTemp('clide-editor-outside-');
|
|
addTearDown(() => outside.deleteSync(recursive: true));
|
|
await File('${outside.path}/secret.txt').writeAsString('secret');
|
|
Link('${sandbox.path}/sneaky').createSync('${outside.path}/secret.txt');
|
|
await expectLater(reg.open('sneaky'), throwsA(isA<PathOutsideRoot>()));
|
|
});
|
|
|
|
test('save rejects a buffer whose path now symlinks outside', () async {
|
|
// Open a legitimate file, then swap a symlink in under its path.
|
|
final buf = await reg.open('victim.txt');
|
|
reg.setContent(buf.id, 'attacker-controlled');
|
|
final outside = await Directory.systemTemp.createTemp('clide-editor-outside-');
|
|
addTearDown(() => outside.deleteSync(recursive: true));
|
|
await File('${outside.path}/target.txt').writeAsString('original');
|
|
Link('${sandbox.path}/victim.txt').createSync('${outside.path}/target.txt');
|
|
await expectLater(reg.save(buf.id), throwsA(isA<PathOutsideRoot>()));
|
|
expect(await File('${outside.path}/target.txt').readAsString(), 'original');
|
|
});
|
|
});
|
|
}
|