Files
termos/test/core/commands/cd.test.ts
T

113 lines
3.1 KiB
TypeScript

import { descriptor as cd } from "@core/commands/cd";
import type { ResultSuccess } from "@core/result";
import { getCurrentSession } from "@core/session";
import { createInitialShellState, withCwd, type ShellState } from "@core/shell_state";
import type { UserInfo } from "@core/user_info";
import { buildTestFS, type FixtureNode } from "@tests/helpers/filesystems_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 users: Map<number, UserInfo> = new Map();
const user: UserInfo = {
name: "root",
uid: 0,
gid: 0,
group: [0],
home: 0,
};
users.set(0, user);
const state: ShellState = createInitialShellState(fs, users, user);
test("changes cwd to home", () => {
withCwd(state, 2);
const output = cd.fn(state, [], []);
const newState = output.state;
const result = output.result;
const session = getCurrentSession(newState);
expect((result as ResultSuccess).exitCode).toBe(0);
expect(session.cwd).toBe(0);
expect(session.pwd).toBe(2);
});
test("changes cwd to pwd", () => {
withCwd(state, 2);
const output = cd.fn(state, [], ["-"]);
const newState = output.state;
const result = output.result;
const session = getCurrentSession(newState);
expect(session.exitCode).toBe(0);
expect(session.cwd).toBe(0);
expect(session.pwd).toBe(2);
});
test("changes cwd to 4", () => {
const output = cd.fn(state, [], ["/sub/bye"]);
const newState = output.state;
const result = output.result;
const session = getCurrentSession(newState);
expect(session.exitCode).toBe(0);
expect(session.cwd).toBe(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"]);
const newState = output.state;
const result = output.result;
const session = getCurrentSession(newState);
console.log(result);
expect(session.exitCode).toBe(0);
expect(session.cwd).toBe(4);
expect(session.pwd).toBe(0);
});
test("returns an error result (tried to enter a file)", () => {
const newState = cd.fn(state, [], ["/hello.txt"]).state;
const session = getCurrentSession(newState);
expect(session.exitCode).toBe(0);
expect(session.cwd).toBe(4);
expect(session.pwd).toBe(0);
});