Skip to content

Commit

Permalink
Merge pull request #124 from zamm-dev/windows-escape-codes
Browse files Browse the repository at this point in the history
Handle recorded Windows terminal escape codes
  • Loading branch information
amosjyng authored Oct 29, 2024
2 parents 7e8c4ed + 9045904 commit 649032b
Show file tree
Hide file tree
Showing 19 changed files with 322 additions and 40 deletions.
27 changes: 27 additions & 0 deletions src-svelte/src/lib/sample-call-testing.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { TauriInvokePlayback } from "./sample-call-testing";

describe("TauriInvokePlayback", () => {
it("should mock matched calls", async () => {
const playback = new TauriInvokePlayback();
playback.addCalls({
request: ["command", { inputArg: "input" }],
response: { outputKey: "output" },
succeeded: true,
});
const result = await playback.mockCall("command", { inputArg: "input" });
expect(result).toEqual({ outputKey: "output" });
});

it("should throw an error for unmatched calls", async () => {
const playback = new TauriInvokePlayback();
playback.addCalls({
request: ["command", { inputArg: "input" }],
response: { outputKey: "output" },
succeeded: true,
});
expect(() => playback.mockCall("command", { inputArg: "wrong" })).toThrow(
'No matching call found for ["command",{"inputArg":"wrong"}].\n' +
'Candidates are ["command",{"inputArg":"input"}]',
);
});
});
19 changes: 12 additions & 7 deletions src-svelte/src/lib/sample-call-testing.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from "fs";
import yaml from "js-yaml";
import { Convert } from "./sample-call";
import { camelCase } from "lodash";
import { camelCase, isArray } from "lodash";

function customAssert(condition: boolean, message?: string): void {
if (!condition) {
Expand Down Expand Up @@ -57,7 +57,11 @@ export function parseSampleCall(sampleFile: string): ParsedCall {
}

function stringify(obj: any): string {
if (typeof obj === "object") {
if (isArray(obj)) {
const items = obj.map((item) => stringify(item));
return `[${items.join(",")}]`;
}
if (obj.constructor === Object) {
return JSON.stringify(obj, Object.keys(obj).sort());
}
return JSON.stringify(obj);
Expand All @@ -75,13 +79,14 @@ export class TauriInvokePlayback {
...args: (string | Record<string, string>)[]
): Promise<Record<string, string>> {
const jsonArgs = stringify(args);
const matchingCallIndex = this.unmatchedCalls.findIndex(
(call) => stringify(call.request) === jsonArgs,
const stringifiedUnmatchedCalls = this.unmatchedCalls.map((call) =>
stringify(call.request),
);
const matchingCallIndex = stringifiedUnmatchedCalls.findIndex(
(call) => call === jsonArgs,
);
if (matchingCallIndex === -1) {
const candidates = this.unmatchedCalls
.map((call) => stringify(call.request))
.join("\n");
const candidates = stringifiedUnmatchedCalls.join("\n");
const errorMessage =
`No matching call found for ${jsonArgs}.\n` +
`Candidates are ${candidates}`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,21 @@ New.parameters = {
],
};

export const NewOnWindows: StoryObj = Template.bind({}) as any;
NewOnWindows.parameters = {
sampleCallFiles: [
"/api/sample-calls/run_command-cmd.yaml",
"/api/sample-calls/send_command_input-cmd-dir.yaml",
],
};

export const InProgress: StoryObj = Template.bind({}) as any;
InProgress.args = {
sessionId: "3717ed48-ab52-4654-9f33-de5797af5118",
command: "bash",
output:
// eslint-disable-next-line max-len
"The default interactive shell is now zsh.\r\nTo update your account to use zsh, please run `chsh -s /bin/zsh`.\r\nFor more details, please visit https://support.apple.com/kb/HT208050.\r\nbash-3.2$ ",
"The default interactive shell is now zsh.\nTo update your account to use zsh, please run `chsh -s /bin/zsh`.\nFor more details, please visit https://support.apple.com/kb/HT208050.\nbash-3.2$ ",
};
InProgress.parameters = {
sampleCallFiles: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import { snackbarError } from "$lib/snackbar/Snackbar.svelte";
import EmptyPlaceholder from "$lib/EmptyPlaceholder.svelte";
import Scrollable from "$lib/Scrollable.svelte";
import { systemInfo } from "$lib/system-info";
export let sessionId: string | undefined = undefined;
export let command: string | undefined = undefined;
Expand Down Expand Up @@ -34,10 +33,9 @@
let result = await runCommand(newInput);
command = newInput;
sessionId = result.id;
output += result.output.trimStart();
output += result.output;
} else {
const inputNewline = $systemInfo?.os === "Windows" ? "\r\n" : "\n";
let result = await sendCommandInput(sessionId, newInput + inputNewline);
let result = await sendCommandInput(sessionId, newInput);
output += result;
}
resizeTerminalView();
Expand Down
30 changes: 16 additions & 14 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ async-trait = "0.1.81"
shlex = "1.3.0"
either = "1.13.0"
portable-pty = "0.8.1"
regex = "1.11.1"
lazy_static = "1.5.0"

[features]
# this feature is used for production builds or when `devPath` points to the filesystem
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/api/sample-calls/run_command-bash.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ response:
{
"id": "3717ed48-ab52-4654-9f33-de5797af5118",
"timestamp": "2024-09-24T16:27:25",
"output": "\r\nThe default interactive shell is now zsh.\r\nTo update your account to use zsh, please run `chsh -s /bin/zsh`.\r\nFor more details, please visit https://support.apple.com/kb/HT208050.\r\nbash-3.2$ "
"output": "The default interactive shell is now zsh.\nTo update your account to use zsh, please run `chsh -s /bin/zsh`.\nFor more details, please visit https://support.apple.com/kb/HT208050.\nbash-3.2$ "
}
sideEffects:
database:
Expand Down
18 changes: 18 additions & 0 deletions src-tauri/api/sample-calls/run_command-cmd.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
request:
- run_command
- >
{
"command": "cmd"
}
response:
message: >
{
"id": "319cc7fd-58cc-4320-ab46-2f0ba11c5402",
"timestamp": "2024-10-17T06:02:13",
"output": "Microsoft Windows [Version 10.0.22631.4169]\n(c) Microsoft Corporation. All rights reserved.\n\nC:\\Users\\Amos Ng\\Documents\\projects\\zamm-dev\\zamm\\src-tauri>"
}
sideEffects:
database:
endStateDump: command-run-cmd
terminal:
recordingFile: windows.cast
2 changes: 1 addition & 1 deletion src-tauri/api/sample-calls/run_command-date.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ response:
{
"id": "38a5e2ea-2222-4913-9b20-2a1c682ab358",
"timestamp": "2024-09-20T11:23:53",
"output": "Friday September 20, 2024 18:23 +0700\r\n"
"output": "Friday September 20, 2024 18:23 +0700\n"
}
sideEffects:
database:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ request:
- >
{
"session_id": "3717ed48-ab52-4654-9f33-de5797af5118",
"input": "python api/sample-terminal-sessions/interleaved.py\n"
"input": "python api/sample-terminal-sessions/interleaved.py"
}
response:
message: >
"python api/sample-terminal-sessions/interleaved.py\r\nstdout\r\nstderr\r\nstdout\r\nbash-3.2$ "
"python api/sample-terminal-sessions/interleaved.py\nstdout\nstderr\nstdout\nbash-3.2$ "
sideEffects:
database:
startStateDump: command-run-bash
Expand Down
17 changes: 17 additions & 0 deletions src-tauri/api/sample-calls/send_command_input-cmd-dir.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
request:
- send_command_input
- >
{
"session_id": "319cc7fd-58cc-4320-ab46-2f0ba11c5402",
"input": "dir"
}
response:
message: >
"dir\n Volume in drive C is Windows\n Volume Serial Number is 30A7-E02E\n\n Directory of C:\\Users\\Amos Ng\\Documents\\projects\\zamm-dev\\zamm\\src-tauri\n\n25/09/2024 05:54 pm <DIR> .\n20/09/2024 07:02 pm <DIR> ..\n14/02/2024 08:37 pm 73 .gitignore\n14/02/2024 08:37 pm 52 .rustfmt.toml\n17/10/2024 12:47 pm <DIR> api\n14/02/2024 08:37 pm <DIR> binaries\n14/02/2024 08:37 pm 90 build.rs\n25/09/2024 05:54 pm 142,536 Cargo.lock\n17/10/2024 12:58 pm 2,102 Cargo.toml\n20/09/2024 07:02 pm 830 clippy.py\n14/02/2024 08:37 pm 244 diesel.toml\n20/09/2024 07:02 pm <DIR> icons\n14/05/2024 03:37 pm 495 Makefile\n20/09/2024 07:02 pm <DIR> migrations\n14/02/2024 08:37 pm <DIR> sounds\n17/10/2024 12:47 pm <DIR> src\n25/06/2024 08:14 pm <DIR> target\n20/09/2024 07:02 pm 1,645 tauri.conf.json\n 9 File(s) 148,067 bytes\n 9 Dir(s) 190,782,652,416 bytes free\n\nC:\\Users\\Amos Ng\\Documents\\projects\\zamm-dev\\zamm\\src-tauri>"
sideEffects:
database:
startStateDump: command-run-cmd
endStateDump: command-run-cmd-dir
terminal:
recordingFile: windows.cast
startingIndex: 1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
INSERT INTO asciicasts VALUES('319cc7fd-58cc-4320-ab46-2f0ba11c5402','2024-10-17 06:02:13','cmd','Windows',replace('{"version":2,"width":80,"height":24,"timestamp":1729144933,"command":"cmd"}\012[0.208,"o","\u001b[?25l\u001b[2J\u001b[m\u001b[HMicrosoft Windows [Version 10.0.22631.4169]\r\n(c) Microsoft Corporation. All rights reserved.\u001b[4;1HC:\\Users\\Amos Ng\\Documents\\projects\\zamm-dev\\zamm\\src-tauri>\u001b]0;C:\\WINDOWS\\system32\\cmd.EXE\u0007\u001b[?25h"]\012[0.208,"i","dir\r\n"]\012[0.41,"o","\u001b[?25ldir\r\n Volume in drive C is Windows\r\n Volume Serial Number is 30A7-E02E\u001b[8;1H Directory of C:\\Users\\Amos Ng\\Documents\\projects\\zamm-dev\\zamm\\src-tauri\u001b[10;1H25/09/2024 05:54 pm <DIR> .\r\n20/09/2024 07:02 pm <DIR> ..\r\n14/02/2024 08:37 pm 73 .gitignore\r\n14/02/2024 08:37 pm 52 .rustfmt.toml\r\n17/10/2024 12:47 pm <DIR> api\r\n14/02/2024 08:37 pm <DIR> binaries\r\n14/02/2024 08:37 pm 90 build.rs\r\n25/09/2024 05:54 pm 142,536 Cargo.lock\r\n17/10/2024 12:58 pm 2,102 Cargo.toml\r\n20/09/2024 07:02 pm 830 clippy.py\r\n14/02/2024 08:37 pm 244 diesel.toml\r\n20/09/2024 07:02 pm <DIR> icons\r\n14/05/2024 03:37 pm 495 Makefile\r\n20/09/2024 07:02 pm <DIR> migrations\r\n14/02/2024 08:37 pm <DIR> sounds\r\u001b]0;C:\\WINDOWS\\system32\\cmd.EXE - dir\u0007\u001b[?25h\n17/10/2024 12:47 pm <DIR> src\r\n25/06/2024 08:14 pm <DIR> target\r\n20/09/2024 07:02 pm 1,645 tauri.conf.json\r\n 9 File(s) 148,067 bytes\r\n 9 Dir(s) 190,782,652,416 bytes free\r\n\u001b]0;C:\\WINDOWS\\system32\\cmd.EXE\u0007\nC:\\Users\\Amos Ng\\Documents\\projects\\zamm-dev\\zamm\\src-tauri>"]','\012',char(10)));
22 changes: 22 additions & 0 deletions src-tauri/api/sample-database-writes/command-run-cmd-dir/dump.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
terminal_sessions:
- id: 319cc7fd-58cc-4320-ab46-2f0ba11c5402
timestamp: 2024-10-17T06:02:13
command: cmd
os: Windows
cast:
header:
version: 2
width: 80
height: 24
timestamp: 1729144933
command: cmd
entries:
- - 0.208
- 'o'
- "\e[?25l\e[2J\e[m\e[HMicrosoft Windows [Version 10.0.22631.4169]\r\n(c) Microsoft Corporation. All rights reserved.\e[4;1HC:\\Users\\Amos Ng\\Documents\\projects\\zamm-dev\\zamm\\src-tauri>\e]0;C:\\WINDOWS\\system32\\cmd.EXE\a\e[?25h"
- - 0.208
- 'i'
- "dir\r\n"
- - 0.41
- 'o'
- "\e[?25ldir\r\n Volume in drive C is Windows\r\n Volume Serial Number is 30A7-E02E\e[8;1H Directory of C:\\Users\\Amos Ng\\Documents\\projects\\zamm-dev\\zamm\\src-tauri\e[10;1H25/09/2024 05:54 pm <DIR> .\r\n20/09/2024 07:02 pm <DIR> ..\r\n14/02/2024 08:37 pm 73 .gitignore\r\n14/02/2024 08:37 pm 52 .rustfmt.toml\r\n17/10/2024 12:47 pm <DIR> api\r\n14/02/2024 08:37 pm <DIR> binaries\r\n14/02/2024 08:37 pm 90 build.rs\r\n25/09/2024 05:54 pm 142,536 Cargo.lock\r\n17/10/2024 12:58 pm 2,102 Cargo.toml\r\n20/09/2024 07:02 pm 830 clippy.py\r\n14/02/2024 08:37 pm 244 diesel.toml\r\n20/09/2024 07:02 pm <DIR> icons\r\n14/05/2024 03:37 pm 495 Makefile\r\n20/09/2024 07:02 pm <DIR> migrations\r\n14/02/2024 08:37 pm <DIR> sounds\r\e]0;C:\\WINDOWS\\system32\\cmd.EXE - dir\a\e[?25h\n17/10/2024 12:47 pm <DIR> src\r\n25/06/2024 08:14 pm <DIR> target\r\n20/09/2024 07:02 pm 1,645 tauri.conf.json\r\n 9 File(s) 148,067 bytes\r\n 9 Dir(s) 190,782,652,416 bytes free\r\n\e]0;C:\\WINDOWS\\system32\\cmd.EXE\a\nC:\\Users\\Amos Ng\\Documents\\projects\\zamm-dev\\zamm\\src-tauri>"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
INSERT INTO asciicasts VALUES('319cc7fd-58cc-4320-ab46-2f0ba11c5402','2024-10-17 06:02:13','cmd','Windows',replace('{"version":2,"width":80,"height":24,"timestamp":1729144933,"command":"cmd"}\012[0.208,"o","\u001b[?25l\u001b[2J\u001b[m\u001b[HMicrosoft Windows [Version 10.0.22631.4169]\r\n(c) Microsoft Corporation. All rights reserved.\u001b[4;1HC:\\Users\\Amos Ng\\Documents\\projects\\zamm-dev\\zamm\\src-tauri>\u001b]0;C:\\WINDOWS\\system32\\cmd.EXE\u0007\u001b[?25h"]','\012',char(10)));
16 changes: 16 additions & 0 deletions src-tauri/api/sample-database-writes/command-run-cmd/dump.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
terminal_sessions:
- id: 319cc7fd-58cc-4320-ab46-2f0ba11c5402
timestamp: 2024-10-17T06:02:13
command: cmd
os: Windows
cast:
header:
version: 2
width: 80
height: 24
timestamp: 1729144933
command: cmd
entries:
- - 0.208
- 'o'
- "\e[?25l\e[2J\e[m\e[HMicrosoft Windows [Version 10.0.22631.4169]\r\n(c) Microsoft Corporation. All rights reserved.\e[4;1HC:\\Users\\Amos Ng\\Documents\\projects\\zamm-dev\\zamm\\src-tauri>\e]0;C:\\WINDOWS\\system32\\cmd.EXE\a\e[?25h"
1 change: 1 addition & 0 deletions src-tauri/src/commands/terminal/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod models;
mod parse;
mod run;
mod send_input;

Expand Down
Loading

0 comments on commit 649032b

Please sign in to comment.