Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions apps/web/src/terminalStateStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,36 @@ import { beforeEach, describe, expect, it } from "vitest";
import { selectThreadTerminalState, useTerminalStateStore } from "./terminalStateStore";

const THREAD_ID = ThreadId.makeUnsafe("thread-1");
const storageBacking = new Map<string, string>();

const storageMock: Storage = {
get length() {
return storageBacking.size;
},
clear() {
storageBacking.clear();
},
getItem(key) {
return storageBacking.get(key) ?? null;
},
key(index) {
return [...storageBacking.keys()][index] ?? null;
},
removeItem(key) {
storageBacking.delete(key);
},
setItem(key, value) {
storageBacking.set(key, value);
},
};

describe("terminalStateStore actions", () => {
beforeEach(() => {
if (typeof localStorage !== "undefined") {
localStorage.clear();
}
Object.defineProperty(globalThis, "localStorage", {
configurable: true,
value: storageMock,
});
localStorage.clear();
useTerminalStateStore.setState({ terminalStateByThreadId: {} });
});

Expand Down
30 changes: 29 additions & 1 deletion apps/web/src/terminalStateStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,34 @@ interface ThreadTerminalState {

const TERMINAL_STATE_STORAGE_KEY = "t3code:terminal-state:v1";

const noopStorage: Storage = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

feels like we have a lot of these mocks now in a lot of different test files. should unify it to one central MemoryStorage that we can use as runtmie fallback and also in all tests

you up for doing this? i know it will increase the scope of the PR a bit but for maintainability would be nice to do.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure :)

Will implement it later (if you want that could be even a different PR)

get length() {
return 0;
},
clear() {},
getItem() {
return null;
},
key() {
return null;
},
removeItem() {},
setItem() {},
};

function getTerminalStateStorage(): Storage {
const storage = globalThis.localStorage;
if (
storage &&
typeof storage.getItem === "function" &&
typeof storage.setItem === "function" &&
typeof storage.removeItem === "function"
) {
return storage;
}
return noopStorage;
}

function normalizeTerminalIds(terminalIds: string[]): string[] {
const ids = [...new Set(terminalIds.map((id) => id.trim()).filter((id) => id.length > 0))];
return ids.length > 0 ? ids : [DEFAULT_THREAD_TERMINAL_ID];
Expand Down Expand Up @@ -542,7 +570,7 @@ export const useTerminalStateStore = create<TerminalStateStoreState>()(
{
name: TERMINAL_STATE_STORAGE_KEY,
version: 1,
storage: createJSONStorage(() => localStorage),
storage: createJSONStorage(getTerminalStateStorage),
partialize: (state) => ({
terminalStateByThreadId: state.terminalStateByThreadId,
}),
Expand Down