import assert from "assert"; import { beforeCursor, Offer } from "./completion.js"; // A condition with the cursor at `cursor`, nothing selected unless `end` says. const field = (value, cursor = value.length, end = cursor) => ({ value, selectionStart: cursor, selectionEnd: end }); const tab = { key: "Tab", shiftKey: false }; const payee = new Offer("pa", { candidates: ["payee", "payee_iban"], insert: "yee" }); Deno.test("D9: what is completed is the text up to the cursor", () => { assert.equal(beforeCursor(field("pa == 1", 2)), "pa"); }); Deno.test("D9: with text selected, the cursor is where the selection starts", () => { assert.equal(beforeCursor(field("payee", 2, 5)), "pa"); }); Deno.test("D9: an offer is for the text it was asked for", () => { assert.equal(payee.isFor(field("pa")), true); assert.equal(payee.isFor(field("pay")), false); assert.equal(payee.isFor(field("pa == 1", 2)), true); }); Deno.test("D9: Tab takes an offer for the text before the cursor", () => { assert.equal(payee.takesTab(tab, field("pa")), true); }); Deno.test("D9: Tab moves on when the text has moved on since the offer", () => { assert.equal(payee.takesTab(tab, field("pay")), false); }); Deno.test("D9: Tab moves on when there is nothing to offer", () => { const none = new Offer("x == ", { candidates: [], insert: "" }); assert.equal(none.takesTab(tab, field("x == ")), false); }); Deno.test("D9: Shift-Tab and other keys never complete", () => { assert.equal(payee.takesTab({ key: "Tab", shiftKey: true }, field("pa")), false); assert.equal(payee.takesTab({ key: "Enter", shiftKey: false }, field("pa")), false); }); Deno.test("D9: Tab with text selected never completes", () => { // "yee" selected, after the very text the offer is for. assert.equal(payee.takesTab(tab, field("payee", 2, 5)), false); }); Deno.test("D9: the hint lists the candidates as offered", () => { assert.equal(payee.hint, "payee payee_iban"); }); Deno.test("D9: an offer inserts what the server said to", () => { assert.equal(payee.insert, "yee"); });