Files
termos/test/core/shell_state.test.ts
ChaosMaker 1ee2a2fe7a - Old tests fixed
- Added a static Fixuture class for test mockups
2026-08-28 16:52:59 +02:00

65 lines
2.3 KiB
TypeScript

import { getCurrentSession } from "@core/session_manager";
import { withCwd, withEnv, withExitCode, withHistory } from "@core/shell_state";
import { Fixture } from "@tests/helpers/state_fixture";
import { expect, test } from "vitest";
const state = Fixture.State;
test("createInitialShellState - initializes successfully", () => {
expect(state.sessionStack.at(0)).toBeDefined();
expect(state.users.get(Fixture.user.uid)).toEqual(Fixture.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(Fixture.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);
});