diff --git a/src/core/session.ts b/src/core/session.ts index be30d0a..04f3627 100644 --- a/src/core/session.ts +++ b/src/core/session.ts @@ -1,4 +1,8 @@ -interface SessionState { +import { type HistoryState } from "./history_state"; +import type { ShellState } from "./shell_state"; +import type { UserInfo } from "./user_info"; + +export interface SessionState { readonly user: UserInfo; readonly envVars: ReadonlyMap; readonly history: HistoryState; @@ -6,3 +10,18 @@ interface SessionState { readonly pwd: number; readonly exitCode: number; } + +export function createInitialSessionState(user: UserInfo): SessionState { + return Object.freeze({ + user, + envVars: Object.freeze(new Map()), + history: Object.freeze([]), + cwd: user.home, + pwd: user.home, + exitCode: 0, + } satisfies SessionState); +} + +export function getCurrentSession(state: ShellState): SessionState { + return state.sessionStack[state.sessionStack.length - 1]!; +} diff --git a/src/core/shell_state.ts b/src/core/shell_state.ts index 289f484..b024d9b 100644 --- a/src/core/shell_state.ts +++ b/src/core/shell_state.ts @@ -1,7 +1,117 @@ import type { FileSystem } from "./filesystem/filesystem"; +import { addHistoryEntry } from "./history_state"; +import { createInitialSessionState, getCurrentSession, type SessionState } from "./session"; +import type { UserInfo } from "./user_info"; -interface ShellState { +export interface ShellState { readonly filesystem: FileSystem; readonly users: ReadonlyMap; - readonly sessionState: ReadonlyArray; + readonly sessionStack: ReadonlyArray; +} + +export function createInitialShellState( + filesystem: FileSystem, + users: ReadonlyMap, + user: UserInfo, +): ShellState { + return Object.freeze({ + filesystem, + users, + sessionStack: Object.freeze([createInitialSessionState(user)]), + }); +} + +export function withCwd(state: ShellState, inode: number): ShellState { + const session = getCurrentSession(state); + + const newSession = Object.freeze({ + user: session.user, + envVars: Object.freeze(session.envVars), + history: Object.freeze(session.history), + cwd: inode, + pwd: session.cwd, + exitCode: session.exitCode, + } satisfies SessionState); + + const sessionStack = [...state.sessionStack]; + + sessionStack[sessionStack.length - 1] = newSession; + + return Object.freeze({ + filesystem: state.filesystem, + users: state.users, + sessionStack: Object.freeze(sessionStack), + } satisfies ShellState); +} + +export function withHistory(state: ShellState, input: string): ShellState { + const session = getCurrentSession(state); + + const newSession = Object.freeze({ + user: session.user, + envVars: Object.freeze(session.envVars), + history: Object.freeze(addHistoryEntry(session.history, input)), + cwd: session.cwd, + pwd: session.pwd, + exitCode: session.exitCode, + } satisfies SessionState); + + const sessionStack = [...state.sessionStack]; + + sessionStack[sessionStack.length - 1] = newSession; + + return Object.freeze({ + filesystem: state.filesystem, + users: state.users, + sessionStack: Object.freeze(sessionStack), + } satisfies ShellState); +} + +export function withEnv(state: ShellState, key: string, value: string): ShellState { + const session = getCurrentSession(state); + + const envVars = new Map(session.envVars); + envVars.set(key, value); + + const newSession = Object.freeze({ + user: session.user, + envVars: Object.freeze(envVars), + history: Object.freeze(session.history), + cwd: session.cwd, + pwd: session.pwd, + exitCode: session.exitCode, + } satisfies SessionState); + + const sessionStack = [...state.sessionStack]; + + sessionStack[sessionStack.length - 1] = newSession; + + return Object.freeze({ + filesystem: state.filesystem, + users: state.users, + sessionStack: Object.freeze(sessionStack), + } satisfies ShellState); +} + +export function withExitCode(state: ShellState, exitCode: number): ShellState { + const session = getCurrentSession(state); + + const newSession = Object.freeze({ + user: session.user, + envVars: Object.freeze(session.envVars), + history: Object.freeze(session.history), + cwd: session.cwd, + pwd: session.pwd, + exitCode: exitCode, + } satisfies SessionState); + + const sessionStack = [...state.sessionStack]; + + sessionStack[sessionStack.length - 1] = newSession; + + return Object.freeze({ + filesystem: state.filesystem, + users: state.users, + sessionStack: Object.freeze(sessionStack), + } satisfies ShellState); } diff --git a/src/core/user_info.ts b/src/core/user_info.ts index 1e1577e..b5754be 100644 --- a/src/core/user_info.ts +++ b/src/core/user_info.ts @@ -1,4 +1,5 @@ -interface UserInfo { +export interface UserInfo { + readonly name: string; readonly uid: number; readonly gid: number; readonly group: readonly number[]; diff --git a/src/core/util/parser.ts b/src/core/util/parser.ts index 067b625..fec82a2 100644 --- a/src/core/util/parser.ts +++ b/src/core/util/parser.ts @@ -1,4 +1,4 @@ -type ParserError = UnclosedQuoteError; +//type ParserError = UnclosedQuoteError; interface UnclosedQuoteError { type: "UnclosedQuote"; @@ -21,7 +21,7 @@ function tokenize(input: string): string[] { } const tokens: string[] = []; - let buffer: string[] = []; + const buffer: string[] = []; let state = State.Normal; let escapeReturnState = State.Normal; diff --git a/test/core/history_state.test.ts b/test/core/history_state.test.ts new file mode 100644 index 0000000..99c6bf9 --- /dev/null +++ b/test/core/history_state.test.ts @@ -0,0 +1,20 @@ +import { addHistoryEntry, getHistoryEntry } from "@core/history_state"; +import { expect, test } from "vitest"; + +const historyState: readonly string[] = ['echo "Hello World!"']; + +test("addHistoryEntry - successfully adds an entry to the array", () => { + const state = addHistoryEntry(historyState, "ls -l"); + + expect(state).toEqual(['echo "Hello World!"', "ls -l"]); +}); + +test("getHistoryEntry - successfully adds returns an entry ", () => { + const state = getHistoryEntry(historyState, 0); + + expect(state).toBe('echo "Hello World!"'); +}); + +test("getHistoryEntry - throws an error because of a nonexistant history entry ", () => { + expect(() => getHistoryEntry(historyState, 999)).toThrow(); +}); diff --git a/test/core/session.test.ts b/test/core/session.test.ts new file mode 100644 index 0000000..fe4a882 --- /dev/null +++ b/test/core/session.test.ts @@ -0,0 +1,21 @@ +import { createInitialSessionState } from "@core/session"; +import type { UserInfo } from "@core/user_info"; +import { expect, test } from "vitest"; + +const user: UserInfo = { + name: "root", + uid: 0, + gid: 0, + group: [0], + home: 0, +}; + +test("createInitialSessionState - successfully creates a new session state", () => { + const state = createInitialSessionState(user); + + expect(state.cwd).toEqual(user.home); + expect(state.pwd).toEqual(user.home); + expect(state.exitCode).toEqual(0); + expect(state.user).toBe(user); + expect(Object.isFrozen(state)).toBe(true); +}); diff --git a/test/core/shell_state.test.ts b/test/core/shell_state.test.ts new file mode 100644 index 0000000..b36b232 --- /dev/null +++ b/test/core/shell_state.test.ts @@ -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 = 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 = map; +const overlay: ReadonlyMap = new Map(); +const fs = FileSystem.init(base, overlay, 0); + +const users: Map = 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); +});