From e88220ac0db71556f8cd7e6df774a34615a02e24 Mon Sep 17 00:00:00 2001 From: Milly Date: Tue, 17 Sep 2024 20:58:30 +0900 Subject: [PATCH] :herb: add tests for `denops#interrupt()` --- .../functions/denops/interrupt_test.ts | 86 +++++++++++++++++++ .../denops/testdata/dummy_interrupt_plugin.ts | 17 ++++ 2 files changed, 103 insertions(+) create mode 100644 tests/denops/runtime/functions/denops/interrupt_test.ts create mode 100644 tests/denops/testdata/dummy_interrupt_plugin.ts diff --git a/tests/denops/runtime/functions/denops/interrupt_test.ts b/tests/denops/runtime/functions/denops/interrupt_test.ts new file mode 100644 index 00000000..c0dd2bb0 --- /dev/null +++ b/tests/denops/runtime/functions/denops/interrupt_test.ts @@ -0,0 +1,86 @@ +import { assert, assertEquals } from "jsr:@std/assert@^1.0.1"; +import { resolveTestDataPath } from "/denops-testdata/resolve.ts"; +import { testHost } from "/denops-testutil/host.ts"; +import { wait } from "/denops-testutil/wait.ts"; + +const scriptInterrupt = resolveTestDataPath("dummy_interrupt_plugin.ts"); + +testHost({ + name: "denops#interrupt()", + mode: "all", + postlude: [ + "let g:__test_denops_events = []", + "autocmd User DenopsPlugin* call add(g:__test_denops_events, expand(''))", + "autocmd User DummyInterruptPlugin:* call add(g:__test_denops_events, expand(''))", + "runtime plugin/denops.vim", + // NOTE: Disable startup on VimEnter. + "autocmd! denops_plugin_internal_startup VimEnter", + ], + fn: async ({ host, t }) => { + await t.step("if the server is not yet running", async (t) => { + await t.step("returns immediately", async () => { + await host.call("denops#interrupt"); + }); + }); + + // Start the server and wait. + await host.call("denops#server#start"); + await wait(() => host.call("eval", "denops#server#status() ==# 'running'")); + + await t.step("if the plugin is not yet loaded", async (t) => { + await t.step("returns immediately", async () => { + await host.call("denops#interrupt"); + }); + }); + + await t.step("if the plugin is loaded", async (t) => { + // Load plugin and wait. + await host.call("execute", [ + "let g:__test_denops_events = []", + `call denops#plugin#load('dummy', '${scriptInterrupt}')`, + ], ""); + await wait(async () => + (await host.call("eval", "g:__test_denops_events") as string[]) + .includes("DenopsPluginPost:dummy") + ); + await host.call("execute", [ + "let g:__test_denops_events = []", + ], ""); + + await t.step("returns immediately", async () => { + await host.call("denops#interrupt"); + }); + + await t.step("sends signal to the plugin", async () => { + await wait(() => host.call("eval", "len(g:__test_denops_events)")); + const events = await host.call( + "eval", + "g:__test_denops_events", + ) as string[]; + assert( + events.some((event) => + event.startsWith("DummyInterruptPlugin:Interrupted:") + ), + `Expected event 'DummyInterruptPlugin:Interrupted:*', but: ${ + JSON.stringify(events) + }`, + ); + }); + + await host.call("denops#request", "dummy", "reset", []); + + await t.step("sends signal to the plugin with reason", async () => { + await host.call("execute", [ + "let g:__test_denops_events = []", + ], ""); + + await host.call("denops#interrupt", "test"); + + await wait(() => host.call("eval", "len(g:__test_denops_events)")); + assertEquals(await host.call("eval", "g:__test_denops_events"), [ + "DummyInterruptPlugin:Interrupted:test", + ]); + }); + }); + }, +}); diff --git a/tests/denops/testdata/dummy_interrupt_plugin.ts b/tests/denops/testdata/dummy_interrupt_plugin.ts new file mode 100644 index 00000000..afacda80 --- /dev/null +++ b/tests/denops/testdata/dummy_interrupt_plugin.ts @@ -0,0 +1,17 @@ +import type { Entrypoint } from "jsr:@denops/core@^7.0.0"; + +export const main: Entrypoint = (denops) => { + function reset(): void { + const signal = denops.interrupted; + signal?.addEventListener("abort", async () => { + await denops.cmd( + `doautocmd User DummyInterruptPlugin:Interrupted:${signal.reason}`, + ); + }, { once: true }); + } + reset(); + + denops.dispatcher = { + reset, + }; +};