@tui-sandbox/library 7.7.0 → 8.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,7 +3,7 @@
3
3
  <head>
4
4
  <meta charset="UTF-8" />
5
5
  <title>tui-sandbox integration tests</title>
6
- <script type="module" crossorigin src="/assets/index-DpW-bZpc.js"></script>
6
+ <script type="module" crossorigin src="/assets/index-_30KjjEK.js"></script>
7
7
  <link rel="stylesheet" crossorigin href="/assets/index-D6fBrqAi.css">
8
8
  </head>
9
9
  <body>
@@ -1,10 +1,13 @@
1
1
  import type { BlockingCommandClientInput, ExCommandClientInput, LuaCodeClientInput } from "../server/server.js";
2
2
  import type { BlockingShellCommandOutput, RunExCommandOutput, RunLuaCodeOutput, StartNeovimGenericArguments, TestDirectory } from "../server/types.js";
3
+ export type GenericNeovimBrowserApi = {
4
+ runBlockingShellCommand(input: BlockingCommandClientInput): Promise<BlockingShellCommandOutput>;
5
+ runLuaCode(input: LuaCodeClientInput): Promise<RunLuaCodeOutput>;
6
+ runExCommand(input: ExCommandClientInput): Promise<RunExCommandOutput>;
7
+ dir: TestDirectory;
8
+ };
3
9
  declare global {
4
10
  interface Window {
5
- startNeovim(startArguments?: StartNeovimGenericArguments): Promise<TestDirectory>;
6
- runBlockingShellCommand(input: BlockingCommandClientInput): Promise<BlockingShellCommandOutput>;
7
- runLuaCode(input: LuaCodeClientInput): Promise<RunLuaCodeOutput>;
8
- runExCommand(input: ExCommandClientInput): Promise<RunExCommandOutput>;
11
+ startNeovim(startArguments?: StartNeovimGenericArguments): Promise<GenericNeovimBrowserApi>;
9
12
  }
10
13
  }
@@ -11,14 +11,17 @@ window.startNeovim = async function (startArgs) {
11
11
  filename: startArgs?.filename ?? "initial-file.txt",
12
12
  startupScriptModifications: startArgs?.startupScriptModifications ?? [],
13
13
  });
14
- return testDirectory;
15
- };
16
- window.runBlockingShellCommand = async function (input) {
17
- return client.runBlockingShellCommand(input);
18
- };
19
- window.runLuaCode = async function (input) {
20
- return client.runLuaCode(input);
21
- };
22
- window.runExCommand = async function (input) {
23
- return client.runExCommand(input);
14
+ const neovimBrowserApi = {
15
+ runBlockingShellCommand(input) {
16
+ return client.runBlockingShellCommand(input);
17
+ },
18
+ runLuaCode(input) {
19
+ return client.runLuaCode(input);
20
+ },
21
+ runExCommand(input) {
22
+ return client.runExCommand(input);
23
+ },
24
+ dir: testDirectory,
25
+ };
26
+ return neovimBrowserApi;
24
27
  };
@@ -21,17 +21,30 @@ import type {
21
21
  TestDirectory,
22
22
  } from "@tui-sandbox/library/dist/src/server/types"
23
23
  import type { OverrideProperties } from "type-fest"
24
+ import type { GenericNeovimBrowserApi } from "../../../library/src/browser/neovim-client.ts"
24
25
  import type { MyTestDirectory, MyTestDirectoryFile } from "../../MyTestDirectory"
25
26
 
26
- export type NeovimContext = TestDirectory<MyTestDirectory>
27
+ /** The api that can be used in tests after a Neovim instance has been started. */
28
+ export type NeovimContext = {
29
+ /** Types text into the terminal, making the terminal application receive
30
+ * the keystrokes as input. Requires neovim to be running. */
31
+ typeIntoTerminal(text: string, options?: Partial<Cypress.TypeOptions>): void
27
32
 
28
- declare global {
29
- interface Window {
30
- startNeovim(startArguments?: MyStartNeovimServerArguments): Promise<NeovimContext>
31
- runBlockingShellCommand(input: BlockingCommandClientInput): Promise<BlockingShellCommandOutput>
32
- runLuaCode(input: LuaCodeClientInput): Promise<RunLuaCodeOutput>
33
- runExCommand(input: ExCommandClientInput): Promise<RunExCommandOutput>
34
- }
33
+ /** Runs a shell command in a blocking manner, waiting for the command to
34
+ * finish before returning. Requires neovim to be running. */
35
+ runBlockingShellCommand(input: BlockingCommandClientInput): Cypress.Chainable<BlockingShellCommandOutput>
36
+
37
+ /** Runs a shell command in a blocking manner, waiting for the command to
38
+ * finish before returning. Requires neovim to be running. */
39
+ runLuaCode(input: LuaCodeClientInput): Cypress.Chainable<RunLuaCodeOutput>
40
+
41
+ /** Run an ex command in neovim.
42
+ * @example "echo expand('%:.')" current file, relative to the cwd
43
+ */
44
+ runExCommand(input: ExCommandClientInput): Cypress.Chainable<RunExCommandOutput>
45
+
46
+ /** The test directory, providing type-safe access to its file and directory structure */
47
+ dir: TestDirectory<MyTestDirectory>
35
48
  }
36
49
 
37
50
  /** Arguments for starting the neovim server. They are built based on your test
@@ -40,21 +53,41 @@ type MyStartNeovimServerArguments = OverrideProperties<
40
53
  StartNeovimGenericArguments,
41
54
  {
42
55
  filename?: MyTestDirectoryFile | { openInVerticalSplits: MyTestDirectoryFile[] }
43
- // NOTE: right now you need to make sure the config-modifications directory exists in your test directory
44
56
  startupScriptModifications?: Array<keyof MyTestDirectory["config-modifications"]["contents"]>
45
57
  }
46
58
  >
47
59
 
48
60
  Cypress.Commands.add("startNeovim", (startArguments?: MyStartNeovimServerArguments) => {
49
61
  cy.window().then(async win => {
50
- testWindow = win
51
- return await win.startNeovim(startArguments)
52
- })
53
- })
62
+ const underlyingNeovim: GenericNeovimBrowserApi = await win.startNeovim(
63
+ startArguments as StartNeovimGenericArguments
64
+ )
65
+ testNeovim = underlyingNeovim
66
+
67
+ // wrap everything so that Cypress can await all the commands
68
+ Cypress.Commands.addAll({
69
+ nvim_runBlockingShellCommand: underlyingNeovim.runBlockingShellCommand,
70
+ nvim_runExCommand: underlyingNeovim.runExCommand,
71
+ nvim_runLuaCode: underlyingNeovim.runLuaCode,
72
+ })
73
+
74
+ const api: NeovimContext = {
75
+ runBlockingShellCommand(input) {
76
+ return cy.nvim_runBlockingShellCommand(input)
77
+ },
78
+ runExCommand(input) {
79
+ return cy.nvim_runExCommand(input)
80
+ },
81
+ runLuaCode(input) {
82
+ return cy.nvim_runLuaCode(input)
83
+ },
84
+ typeIntoTerminal(text, options) {
85
+ cy.typeIntoTerminal(text, options)
86
+ },
87
+ dir: underlyingNeovim.dir as TestDirectory<MyTestDirectory>,
88
+ }
54
89
 
55
- Cypress.Commands.add("runBlockingShellCommand", (input: BlockingCommandClientInput) => {
56
- cy.window().then(async win => {
57
- return await win.runBlockingShellCommand(input)
90
+ return api
58
91
  })
59
92
  })
60
93
 
@@ -64,19 +97,7 @@ Cypress.Commands.add("typeIntoTerminal", (text: string, options?: Partial<Cypres
64
97
  cy.get("textarea").focus().type(text, options)
65
98
  })
66
99
 
67
- Cypress.Commands.add("runLuaCode", (input: LuaCodeClientInput) => {
68
- cy.window().then(async win => {
69
- return await win.runLuaCode(input)
70
- })
71
- })
72
-
73
- Cypress.Commands.add("runExCommand", (input: ExCommandClientInput) => {
74
- cy.window().then(async win => {
75
- return await win.runExCommand(input)
76
- })
77
- })
78
-
79
- let testWindow: Window | undefined
100
+ let testNeovim: GenericNeovimBrowserApi | undefined
80
101
 
81
102
  before(function () {
82
103
  // disable Cypress's default behavior of logging all XMLHttpRequests and
@@ -96,28 +117,34 @@ declare global {
96
117
 
97
118
  /** Runs a shell command in a blocking manner, waiting for the command to
98
119
  * finish before returning. Requires neovim to be running. */
99
- runBlockingShellCommand(input: BlockingCommandClientInput): Chainable<BlockingShellCommandOutput>
120
+ nvim_runBlockingShellCommand(input: BlockingCommandClientInput): Chainable<BlockingShellCommandOutput>
100
121
 
101
- runLuaCode(input: LuaCodeClientInput): Chainable<RunLuaCodeOutput>
122
+ nvim_runLuaCode(input: LuaCodeClientInput): Chainable<RunLuaCodeOutput>
102
123
 
103
124
  /** Run an ex command in neovim.
104
125
  * @example "echo expand('%:.')" current file, relative to the cwd
105
126
  */
106
- runExCommand(input: ExCommandClientInput): Chainable<RunExCommandOutput>
127
+ nvim_runExCommand(input: ExCommandClientInput): Chainable<RunExCommandOutput>
107
128
  }
108
129
  }
109
130
  }
110
131
 
111
132
  afterEach(async () => {
112
- if (!testWindow) return
113
- const timeout = new Promise<void>((resolve, reject) =>
114
- setTimeout(() => {
115
- Cypress.log({ name: "timeout when waiting for :messages to finish. Neovim might be stuck." })
116
- reject("timeout when waiting for :messages to finish. Neovim might be stuck.")
133
+ if (!testNeovim) return
134
+
135
+ let timeoutId: NodeJS.Timeout | undefined = undefined
136
+ const timeout = new Promise<void>((_, reject) => {
137
+ timeoutId = setTimeout(() => {
138
+ Cypress.log({ name: "timeout when waiting for :messages to finish. Neovim might be stuck or showing a message." })
139
+ reject(new Error("timeout when waiting for :messages to finish. Neovim might be stuck or showing a message."))
117
140
  }, 5_000)
118
- )
141
+ })
119
142
 
120
- await Promise.race([timeout, testWindow.runExCommand({ command: "messages" })])
143
+ try {
144
+ await Promise.race([timeout, testNeovim.runExCommand({ command: "messages" })])
145
+ } finally {
146
+ clearTimeout(timeoutId) // Ensure the timeout is cleared
147
+ }
121
148
  })
122
149
  `;
123
150
  const options = await resolveConfig(__filename);