Skip to content

Commit

Permalink
update deno test
Browse files Browse the repository at this point in the history
  • Loading branch information
sigmaSd committed Mar 24, 2024
1 parent 1437c99 commit 9cb0125
Showing 1 changed file with 26 additions and 33 deletions.
59 changes: 26 additions & 33 deletions tests/deno_bot_test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
#!/usr/bin/env -S deno run --unstable --allow-all
import { Pty } from "https://deno.land/x/[email protected]/mod.ts";
import { stripAnsiCode } from "https://deno.land/[email protected]/fmt/colors.ts";
import {
assertEquals,
assertMatch,
} from "https://deno.land/[email protected]/assert/mod.ts";
#!/usr/bin/env -S deno run --unstable-ffi --allow-all
import { Pty } from "jsr:@sigma/[email protected]";
import { stripAnsiCode } from "jsr:@std/[email protected]/colors";
import { assertEquals, assertMatch } from "jsr:@std/[email protected]";

const ENCODER = new TextEncoder();

Expand All @@ -16,56 +13,52 @@ if (import.meta.main) {
});

while (true) {
let input = await pty.read();
if (input === undefined) break;
let { data: input, done } = await pty.read();
if (done) break;
input = stripAnsiCode(input);

if (input.includes("In:")) break;
await sleep(100);
}

const write = async (input: string) => await pty.write(input + "\n\r");
const write = async (input: string) => await pty.write(`${input}\n\r`);
const evalRs = async (input: string) => {
await write(input);
// detect output
// the plan is:
// In: ...
// ... // we want his line
// ... // and this line
// In: ...
// The output is all the part between 2 `In:`
//
// Should not be used with input that gives empty output
// like rust statements
let out = "";
let end_detect = 0;
// TODO
let lastResult = "";
let idx = 0;
while (true) {
let o = await pty.read();
if (o === undefined) break;
o = stripAnsiCode(o);
if (!o.startsWith("In:")) {
end_detect += 1;
out += o;
} else if (end_detect >= 1 && o.startsWith("In:")) {
break;
let { data: output, done } = await pty.read();
if (done) break;
output = stripAnsiCode(output).trim();
if (output && output !== "In:") lastResult = output;

if (!output && lastResult) {
idx++;
} else {
end_detect = 0;
idx = 0;
}
if (idx === 5) {
const result = lastResult.replace(/^Out:/, "").trim();
return result;
}
await sleep(100);
}
const result = out!.replace(/^Out:/, "").trim();
return result;
// not really needed
return "";
};

const test = async (
input: string,
expected: string | RegExp,
) => {
Deno.stdout.write(ENCODER.encode("eval: " + input));
Deno.stdout.write(ENCODER.encode(`eval: ${input}`));
const output = await evalRs(input);
// try catch just to add a new line before the error
try {
if (typeof expected == "string") {
if (typeof expected === "string") {
assertEquals(
output,
expected,
Expand Down

0 comments on commit 9cb0125

Please sign in to comment.