Unit tests go to tests/unit/<area>/, browser smoke tests to tests/smoke/<area>/, and tests against a running desktop app to tests/native/. Files named after dates, work passes, or Kanban cards get feature names, for example editorOctober12 -> editor/footnotes. scripts/ keeps the build, release, and package tools. Plugin repositories import scripts/packagePlugin.mjs, and the Terminal smoke test imports scripts/pluginMocks.mjs, which now re-exports the harness copy. The unit tests get their own type check (tests/tsconfig.json) with Node types. pnpm check runs it after the app check. Add @types/node and fix the three test mocks that it reported.
36 lines
3.1 KiB
TypeScript
36 lines
3.1 KiB
TypeScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { CompletionHub, eligible } from '../../../src/lib/completion/hub.ts';
|
|
import type { CompletionSnapshot } from '../../../src/lib/completion/contracts.ts';
|
|
const snapshot = (): CompletionSnapshot => ({ documentId: 'doc', revision: 1, text: 'The ', caret: 4, selectionEnd: 4, focused: true, kind: 'prose' });
|
|
const tick = () => new Promise(resolve => setTimeout(resolve, 0));
|
|
test('sensitive fields, composition, selections, code blocks and link targets are excluded', () => {
|
|
assert(eligible(snapshot()));
|
|
for (const change of [{ sensitive: true }, { composing: true }, { readOnly: true }, { selectionEnd: 0 }, { focused: false }]) assert(!eligible({ ...snapshot(), ...change }));
|
|
for (const text of ['```js\nlet ', '[name](http', '| cell ', '`source', '---\nname: ']) assert(!eligible({ ...snapshot(), text, caret: text.length, selectionEnd: text.length }));
|
|
assert(eligible({ ...snapshot(), kind: 'code', text: 'let ', caret: 4 }));
|
|
});
|
|
test('a new revision blocks stale insertion even when the provider ignores cancellation', async () => {
|
|
const hub = new CompletionHub(); let state = snapshot(), emit!: (text: string) => void, finish!: (text: string) => void, visible = '', edits = 0;
|
|
hub.registerProvider({ id: 'model', complete: (_request, publish) => { emit = publish; return new Promise(resolve => finish = resolve); } });
|
|
const field = hub.registerSurface({ id: 'field', snapshot: () => state, preview: text => visible = text || '', insert: () => { edits++; return true; } });
|
|
field.request(); emit('harbor'); assert.equal(visible, 'harbor');
|
|
state = { ...state, revision: 2, text: 'The old ', caret: 8, selectionEnd: 8 };
|
|
assert.equal(field.accept(), false); finish('late'); await tick(); assert.equal(visible, ''); assert.equal(edits, 0); field.dispose();
|
|
});
|
|
test('acceptance is explicit and provider removal clears all surfaces', async () => {
|
|
const hub = new CompletionHub(); let visible = '', inserted = '';
|
|
const remove = hub.registerProvider({ id: 'model', complete: async () => 'quiet harbor' });
|
|
const field = hub.registerSurface({ id: 'field', snapshot, preview: text => visible = text || '', insert: text => { inserted = text; return true; } });
|
|
field.request(); await tick(); assert.equal(inserted, ''); assert.equal(visible, 'quiet harbor');
|
|
assert(field.accept(true)); assert.equal(inserted, 'quiet '); assert.equal(visible, '');
|
|
field.request(); await tick(); remove(); assert.equal(visible, ''); assert.equal(field.accept(), false); field.dispose();
|
|
});
|
|
test('moving between fields cancels the previous request', async () => {
|
|
const hub = new CompletionHub(); const signals: AbortSignal[] = [];
|
|
hub.registerProvider({ id: 'model', complete: request => { signals.push(request.signal); return new Promise(() => {}); } });
|
|
const a = hub.registerSurface({ id: 'a', snapshot, preview() {}, insert: () => true });
|
|
const b = hub.registerSurface({ id: 'b', snapshot, preview() {}, insert: () => true });
|
|
a.request(); b.request(); assert(signals[0].aborted); assert(!signals[1].aborted); a.dispose(); b.dispose();
|
|
});
|