ShellState, SessionState implemented, all tested, and passing
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
import { FileSystem } from "@core/filesystem/filesystem";
|
||||
import { TreeNodeType, type DirectoryNode, type TreeNode } from "@core/filesystem/treenode";
|
||||
import { getCurrentSession } from "@core/session";
|
||||
import { createInitialShellState, withCwd, withEnv, withExitCode, withHistory } from "@core/shell_state";
|
||||
import type { UserInfo } from "@core/user_info";
|
||||
import { expect, test } from "vitest";
|
||||
|
||||
const map: Map<number, TreeNode> = new Map();
|
||||
const time = Date.now();
|
||||
|
||||
const root: DirectoryNode = {
|
||||
type: TreeNodeType.Directory,
|
||||
inode: 0,
|
||||
name: "root",
|
||||
parent: 0,
|
||||
mode: 0o555,
|
||||
owner: 0,
|
||||
group: 0,
|
||||
mtime: time,
|
||||
atime: time,
|
||||
ctime: time,
|
||||
children: {},
|
||||
};
|
||||
map.set(0, root);
|
||||
|
||||
const base: ReadonlyMap<number, TreeNode> = map;
|
||||
const overlay: ReadonlyMap<number, TreeNode | null> = new Map();
|
||||
const fs = FileSystem.init(base, overlay, 0);
|
||||
|
||||
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 = createInitialShellState(fs, users, user);
|
||||
|
||||
test("createInitialShellState - initializes successfully", () => {
|
||||
expect(state.sessionStack.at(0)).toBeDefined();
|
||||
|
||||
expect(state.users.get(user.uid)).toEqual(user);
|
||||
expect(Object.isFrozen(state)).toBe(true);
|
||||
});
|
||||
|
||||
test("fails - tries to mutate a frozen immutable object", () => {
|
||||
expect(() => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
(state as any).sessionStack = [];
|
||||
}).toThrow();
|
||||
});
|
||||
|
||||
test("getCurrentSession - successfully returns the current (top most) session from the stack", () => {
|
||||
const session = getCurrentSession(state);
|
||||
|
||||
expect(session.cwd).toBe(0);
|
||||
expect(session.pwd).toBe(0);
|
||||
expect(session.exitCode).toBe(0);
|
||||
expect(session.history).toEqual([]);
|
||||
expect(session.envVars.size).toBe(0);
|
||||
expect(session.user).toBe(user);
|
||||
});
|
||||
|
||||
test("withCwd", () => {
|
||||
const nstate = withCwd(state, 1);
|
||||
const result = getCurrentSession(nstate);
|
||||
expect(result.cwd).toBe(1);
|
||||
expect(result.pwd).toBe(0);
|
||||
expect(nstate.filesystem).toBe(state.filesystem);
|
||||
expect(getCurrentSession(state).cwd).toBe(0);
|
||||
});
|
||||
|
||||
test("withEnv", () => {
|
||||
const nstate = withEnv(state, "foo", "Hello World!");
|
||||
const result = getCurrentSession(nstate);
|
||||
expect(result.envVars.get("foo")).toBe("Hello World!");
|
||||
expect(nstate.filesystem).toBe(state.filesystem);
|
||||
expect(getCurrentSession(state).envVars.get("foo")).toBeUndefined();
|
||||
});
|
||||
|
||||
test("withHistory", () => {
|
||||
const nstate = withHistory(state, 'echo "Hi!"');
|
||||
const result = getCurrentSession(nstate);
|
||||
expect(result.history).toContain('echo "Hi!"');
|
||||
expect(nstate.filesystem).toBe(state.filesystem);
|
||||
expect(getCurrentSession(state).history).not.toContain('echo "Hi!"');
|
||||
});
|
||||
|
||||
test("withExitCode", () => {
|
||||
const nstate = withExitCode(state, 10);
|
||||
const result = getCurrentSession(nstate);
|
||||
expect(result.exitCode).toEqual(10);
|
||||
expect(nstate.filesystem).toBe(state.filesystem);
|
||||
expect(getCurrentSession(state).exitCode).toEqual(0);
|
||||
});
|
||||
Reference in New Issue
Block a user