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
+2
View File
@@ -1,11 +1,13 @@
import type { CommandDescriptor } from "./command";
import { descriptor as cd } from "./commands/cd";
import { descriptor as echo } from "./commands/echo";
import { descriptor as testasync } from "./commands/testasync";
const reg = new Map<string, CommandDescriptor>();
//reg.set(descriptor.name, descriptor <- imported const from command implementation file);
reg.set(echo.name, echo);
reg.set(cd.name, cd);
reg.set(testasync.name, testasync);
Object.freeze(reg);
export const COMMAND_REGISTRY: ReadonlyMap<string, CommandDescriptor> = reg;
+7 -1
View File
@@ -1,4 +1,5 @@
import type { CommandDescriptor, CommandOutput } from "@core/command";
import type { CommandIO } from "@core/command_io";
import type { FileSystem } from "@core/filesystem/filesystem";
import { createErrorResult, createSuccessResult } from "@core/result";
import { getCurrentSession } from "@core/session_manager";
@@ -14,7 +15,12 @@ import { resolveInode, resolvePath } from "@core/util/path_resolver";
export const COMMAND_NAME = "cd";
function main(state: ShellState, __flags: string[], args: string[]): CommandOutput<ShellError> {
async function main(
__io: CommandIO,
state: ShellState,
__flags: string[],
args: string[],
): Promise<CommandOutput<ShellError>> {
if (args.length > 1) {
return {
state,
+9 -3
View File
@@ -1,4 +1,5 @@
import type { CommandDescriptor, CommandOutput } from "@core/command";
import type { CommandIO } from "@core/command_io";
import type { ResultSuccess } from "@core/result";
import type { ShellState } from "@core/shell_state";
import type { ShellError } from "@core/util/errors";
@@ -7,16 +8,21 @@ import { createImmutableBuffer } from "@core/util/immutable_buffer";
export const COMMAND_NAME = "echo";
//Need to implement escape sequences
function main(state: ShellState, flags: string[], args: string[]): CommandOutput<ShellError> {
async function main(
io: CommandIO,
state: ShellState,
__flags: string[],
args: string[],
): Promise<CommandOutput<ShellError>> {
const joined = args.join(" ");
const encoded = new TextEncoder().encode(joined);
const stdout = createImmutableBuffer(encoded);
io.writeStdout(encoded);
const result: ResultSuccess = {
type: "success",
exitCode: 0,
stdout,
stdout: createImmutableBuffer(new Uint8Array()),
stderr: createImmutableBuffer(new Uint8Array()),
commandName: COMMAND_NAME,
};
+44
View File
@@ -0,0 +1,44 @@
import type { CommandDescriptor, CommandOutput } from "@core/command";
import type { CommandIO } from "@core/command_io";
import type { ResultSuccess } from "@core/result";
import type { ShellState } from "@core/shell_state";
import type { ShellError } from "@core/util/errors";
import { createImmutableBuffer } from "@core/util/immutable_buffer";
const COMMAND_NAME = "async";
async function main(
io: CommandIO,
state: ShellState,
__flags: string[],
__args: string[],
): Promise<CommandOutput<ShellError>> {
while (true) {
const out = await io.getSignal();
if (out.type === "INPUT") {
const encoded = new TextEncoder().encode(("user pressed: " + out.payload + "\n") as string);
io.writeStdout(encoded);
if (out.payload === "ctrl+c") {
const result: ResultSuccess = {
type: "success",
exitCode: 0,
stdout: createImmutableBuffer(new Uint8Array()),
stderr: createImmutableBuffer(new Uint8Array()),
commandName: COMMAND_NAME,
};
return {
state: state,
result,
};
}
}
}
}
export const descriptor = {
fn: main,
name: COMMAND_NAME,
} satisfies CommandDescriptor;
+1
View File
@@ -38,6 +38,7 @@ const EXIT_CODE_MAP: Record<string, number> = {
GenericError: 1,
InvalidArguments: 2,
ParseError: 62,
UsageError: 63,
PathResolutionError: 64,
DataFormatError: 65,