WRITE UNIT TESTS!!!

first async command working, no unit tests done AGAING
This commit is contained in:
2026-08-23 06:39:37 +02:00
parent e313aa5b9d
commit 268729bb62
18 changed files with 331 additions and 93 deletions
+26 -10
View File
@@ -1,3 +1,4 @@
import type { CommandIO } from "@core/command_io";
import { descriptor as cd } from "@core/commands/cd";
import type { ResultError, ResultSuccess } from "@core/result";
import type { UserInfo } from "@core/session_manager";
@@ -44,6 +45,21 @@ const testTree: FixtureNode = {
const fs = buildTestFS(testTree);
const stdout: Uint8Array[] = [];
const stderr: Uint8Array[] = [];
const io: CommandIO = {
writeStdout: (data: Uint8Array) => {
stdout.push(data);
},
writeStderr: (data: Uint8Array) => {
stderr.push(data);
},
getSignal: () => {
return new Promise(() => {});
},
};
const users: Map<number, UserInfo> = new Map();
const user: UserInfo = {
username: "root",
@@ -56,9 +72,9 @@ users.set(0, user);
const state: ShellState = createInitialShellState(fs, users, user);
test("changes cwd to home", () => {
test("changes cwd to home", async () => {
let newState = withCwd(state, 2);
const output = cd.fn(newState, [], []);
const output = await cd.fn(io, newState, [], []);
newState = output.state;
const result = output.result;
const session = getCurrentSession(newState);
@@ -67,10 +83,10 @@ test("changes cwd to home", () => {
expect(session.cwd).toBe(0);
expect(session.pwd).toBe(2);
});
test("changes cwd to pwd", () => {
test("changes cwd to pwd", async () => {
let newState = withCwd(state, 2);
const output = cd.fn(newState, [], ["-"]);
const output = await cd.fn(io, newState, [], ["-"]);
newState = output.state;
const result = output.result;
const session = getCurrentSession(newState);
@@ -79,8 +95,8 @@ test("changes cwd to pwd", () => {
expect(session.cwd).toBe(0);
expect(session.pwd).toBe(2);
});
test("changes cwd to 4", () => {
const output = cd.fn(state, [], ["/sub/bye"]);
test("changes cwd to 4", async () => {
const output = await cd.fn(io, state, [], ["/sub/bye"]);
const newState = output.state;
const result = output.result;
const session = getCurrentSession(newState);
@@ -90,8 +106,8 @@ test("changes cwd to 4", () => {
expect(session.pwd).toBe(0);
});
test("returns an error result (tried to enter a non existent location)", () => {
const output = cd.fn(state, [], ["/not/real"]);
test("returns an error result (tried to enter a non existent location)", async () => {
const output = await cd.fn(io, state, [], ["/not/real"]);
const newState = output.state;
const result = output.result;
const session = getCurrentSession(newState);
@@ -101,8 +117,8 @@ test("returns an error result (tried to enter a non existent location)", () => {
expect(session.pwd).toBe(0);
});
test("returns an error result (tried to enter a file)", () => {
const output = cd.fn(state, [], ["/hello.txt"]);
test("returns an error result (tried to enter a file)", async () => {
const output = await cd.fn(io, state, [], ["/hello.txt"]);
const newState = output.state;
const result = output.result;
const session = getCurrentSession(newState);
+19 -3
View File
@@ -1,3 +1,4 @@
import type { CommandIO } from "@core/command_io";
import { descriptor as echo } from "@core/commands/echo";
import type { ResultSuccess } from "@core/result";
import type { UserInfo } from "@core/session_manager";
@@ -42,6 +43,21 @@ const testTree: FixtureNode = {
const fs = buildTestFS(testTree);
const stdout: Uint8Array[] = [];
const stderr: Uint8Array[] = [];
const io: CommandIO = {
writeStdout: (data: Uint8Array) => {
stdout.push(data);
},
writeStderr: (data: Uint8Array) => {
stderr.push(data);
},
getSignal: () => {
return new Promise(() => {});
},
};
const users: Map<number, UserInfo> = new Map();
const user: UserInfo = {
username: "root",
@@ -54,9 +70,9 @@ users.set(0, user);
const state: ShellState = createInitialShellState(fs, users, user);
test("returns a Result of type success", () => {
const output = echo.fn(state, [], ["hello", "world"]);
const string = new TextDecoder().decode(output.result.stdout.toArray());
test("returns a Result of type success", async () => {
const output = await echo.fn(io, state, [], ["hello", "world"]);
const string = new TextDecoder().decode(stdout[0]);
expect(output.result.type).toBe("success");
expect((output.result as ResultSuccess).exitCode).toBe(0);
+79
View File
@@ -0,0 +1,79 @@
import type { UserInfo } from "@core/session_manager";
import { createInitialShellState, type ShellState } from "@core/shell_state";
import { Shell } from "@shell/shell";
import { buildTestFS, type FixtureNode } from "@tests/helpers/filesystems_fixture";
import { test } from "vitest";
const testTree: FixtureNode = {
inode: 0,
name: "root",
type: "Dir",
children: [
{
inode: 1,
name: "hello.txt",
type: "File",
content: "Goodbye World!",
children: [],
},
{
inode: 2,
name: "sub",
type: "Dir",
children: [
{
inode: 3,
name: "world.txt",
type: "File",
content: "Goodbye!",
children: [],
},
{
inode: 4,
name: "bye",
type: "Dir",
children: [],
},
],
},
],
};
const fs = buildTestFS(testTree);
const stdout: Uint8Array[] = [];
const users: Map<number, UserInfo> = new Map();
const user: UserInfo = {
username: "root",
uid: 0,
gid: 0,
group: [0],
home: 0,
};
users.set(0, user);
const state: ShellState = createInitialShellState(fs, users, user);
test("returns a Result of type success", async () => {
await Shell.init(state);
let result;
Shell.Instance?.on("result", payload => {
const decoder = new TextDecoder();
console.log(decoder.decode(payload.result.stdout.toArray()));
console.log(payload);
result = payload.result;
});
await Shell.Instance?.execute("async");
Shell.Instance?.sendInput("a");
Shell.Instance?.sendInput("b");
Shell.Instance?.sendInput("c");
Shell.Instance?.sendInput("d");
Shell.Instance?.sendInput(
"right now technically anything goes because its just a plain string however i plan on improving this system a bit later on",
);
Shell.Instance?.sendInput("ctrl+c");
});
+10 -2
View File
@@ -56,10 +56,18 @@ const state: ShellState = createInitialShellState(fs, users, user);
describe("Shell tests", () => {
test("Successfully executes the cd command", async () => {
await Shell.init(state);
const result = await Shell.Instance?.Execute("cd /sub");
let result;
Shell.Instance?.on("result", payload => {
console.log(payload);
result = payload.result;
});
await Shell.Instance?.execute("cd /sub");
const statee = Shell.Instance?.State;
expect(result?.type).toEqual("success");
expect(result!.type).toEqual("success");
expect(getCurrentSession(statee!).history.length).toBe(1);
expect(getCurrentSession(statee!).history[0]).toEqual("cd /sub");
expect(getCurrentSession(statee!).cwd).toBe(2);