- Old tests fixed

- Added a static Fixuture class for test mockups
This commit is contained in:
2026-08-28 16:52:59 +02:00
parent 310218663d
commit 1ee2a2fe7a
13 changed files with 161 additions and 306 deletions
+6 -43
View File
@@ -3,48 +3,11 @@ import { descriptor as cd } from "@core/commands/cd";
import type { ResultError, ResultSuccess } from "@core/result";
import type { UserInfo } from "@core/session_manager";
import { getCurrentSession } from "@core/session_manager";
import { createInitialShellState, withCwd, type ShellState } from "@core/shell_state";
import { withCwd, type ShellState } from "@core/shell_state";
import type { PathResolutionError } from "@core/util/errors";
import { buildTestFS, type FixtureNode } from "@tests/helpers/filesystems_fixture";
import { Fixture } from "@tests/helpers/state_fixture";
import { expect, 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 stderr: Uint8Array[] = [];
@@ -70,7 +33,7 @@ const user: UserInfo = {
};
users.set(0, user);
const state: ShellState = createInitialShellState(fs, users, user);
const state: ShellState = Fixture.State;
test("changes cwd to home", async () => {
let newState = withCwd(state, 2);
@@ -80,7 +43,7 @@ test("changes cwd to home", async () => {
const session = getCurrentSession(newState);
expect((result as ResultSuccess).exitCode).toBe(0);
expect(session.cwd).toBe(0);
expect(session.cwd).toBe(Fixture.user.home);
expect(session.pwd).toBe(2);
});
test("changes cwd to pwd", async () => {
@@ -96,7 +59,7 @@ test("changes cwd to pwd", async () => {
expect(session.pwd).toBe(2);
});
test("changes cwd to 4", async () => {
const output = await cd.fn(io, state, [], ["/sub/bye"]);
const output = await cd.fn(io, state, [], ["/folder2/subdir2"]);
const newState = output.state;
const result = output.result;
const session = getCurrentSession(newState);
@@ -118,7 +81,7 @@ test("returns an error result (tried to enter a non existent location)", async (
});
test("returns an error result (tried to enter a file)", async () => {
const output = await cd.fn(io, state, [], ["/hello.txt"]);
const output = await cd.fn(io, state, [], ["/some-file"]);
const newState = output.state;
const result = output.result;
const session = getCurrentSession(newState);
+5 -52
View File
@@ -1,48 +1,10 @@
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";
import { createInitialShellState, type ShellState } from "@core/shell_state";
import { buildTestFS, type FixtureNode } from "@tests/helpers/filesystems_fixture";
import { type ShellState } from "@core/shell_state";
import { Fixture } from "@tests/helpers/state_fixture";
import { expect, 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 stderr: Uint8Array[] = [];
@@ -58,17 +20,7 @@ const io: CommandIO = {
},
};
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);
const state: ShellState = Fixture.State;
test("returns a Result of type success", async () => {
const output = await echo.fn(io, state, [], ["hello", "world"]);
@@ -76,5 +28,6 @@ test("returns a Result of type success", async () => {
expect(output.result.type).toBe("success");
expect((output.result as ResultSuccess).exitCode).toBe(0);
expect(string).toBe("hello world");
// Fix: added a newline character
expect(string).toEqual("hello world\n");
});
+14 -57
View File
@@ -1,59 +1,12 @@
import type { UserInfo } from "@core/session_manager";
import { createInitialShellState, type ShellState } from "@core/shell_state";
import type { ResultError } from "@core/result";
import { type ShellState } from "@core/shell_state";
import { SIGNAL_MAP } from "@core/signal";
import type { SignalTerminationError } from "@core/util/errors";
import { Shell } from "@shell/shell";
import { buildTestFS, type FixtureNode } from "@tests/helpers/filesystems_fixture";
import { test } from "vitest";
import { Fixture } from "@tests/helpers/state_fixture";
import { expect, 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);
const state: ShellState = Fixture.State;
test("returns a Result of type success", async () => {
await Shell.init(state);
@@ -61,10 +14,14 @@ test("returns a Result of type success", async () => {
let result;
Shell.Instance?.on("result", payload => {
const decoder = new TextDecoder();
console.log(decoder.decode(payload.result.stdout.toArray()));
console.log(payload);
//const decoder = new TextDecoder();
//console.log(decoder.decode(payload.result.stdout.toArray()));
//console.log(payload);
result = payload.result;
expect(result.type).toBe("error");
expect((result as ResultError<SignalTerminationError>).error.exitCode).toEqual(130);
expect((result as ResultError<SignalTerminationError>).error.signalNumber).toEqual(SIGNAL_MAP.SIGINT);
});
await Shell.Instance?.execute("async");