Skip to content
Closed
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
68 changes: 67 additions & 1 deletion apps/web/src/appSettings.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,78 @@
import { describe, expect, it } from "vitest";
import { beforeEach, describe, expect, it, vi } from "vitest";

import {
APP_SETTINGS_STORAGE_KEY,
getAppSettingsSnapshot,
getAppModelOptions,
getSlashModelOptions,
normalizeCustomModelSlugs,
resolveAppModelSelection,
} from "./appSettings";

function getWindowForTest(): Window & typeof globalThis {
const testGlobal = globalThis as typeof globalThis & {
window?: Window & typeof globalThis;
};
if (!testGlobal.window) {
testGlobal.window = {} as Window & typeof globalThis;
}
return testGlobal.window;
}

function createStorage(): Storage {
const values = new Map<string, string>();
return {
get length() {
return values.size;
},
clear() {
values.clear();
},
getItem(key) {
return values.get(key) ?? null;
},
key(index) {
return [...values.keys()][index] ?? null;
},
removeItem(key) {
values.delete(key);
},
setItem(key, value) {
values.set(key, value);
},
};
}

beforeEach(() => {
const testWindow = getWindowForTest();
Object.defineProperty(testWindow, "localStorage", {
configurable: true,
value: createStorage(),
});
testWindow.addEventListener = vi.fn();
testWindow.removeEventListener = vi.fn();
});

describe("getAppSettingsSnapshot", () => {
it("includes newThreadUsesNewWorktree in the default settings", () => {
expect(getAppSettingsSnapshot().newThreadUsesNewWorktree).toBe(false);
});

it("applies schema defaults when older persisted payloads omit new settings", () => {
getWindowForTest().localStorage.setItem(
APP_SETTINGS_STORAGE_KEY,
JSON.stringify({
confirmThreadDelete: false,
}),
);

expect(getAppSettingsSnapshot()).toMatchObject({
confirmThreadDelete: false,
newThreadUsesNewWorktree: false,
});
});
});

describe("normalizeCustomModelSlugs", () => {
it("normalizes aliases, removes built-ins, and deduplicates values", () => {
expect(
Expand Down
15 changes: 13 additions & 2 deletions apps/web/src/appSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Option, Schema } from "effect";
import { type ProviderKind } from "@t3tools/contracts";
import { getDefaultModel, getModelOptions, normalizeModelSlug } from "@t3tools/shared/model";

const APP_SETTINGS_STORAGE_KEY = "t3code:app-settings:v1";
export const APP_SETTINGS_STORAGE_KEY = "t3code:app-settings:v1";
const MAX_CUSTOM_MODEL_COUNT = 32;
export const MAX_CUSTOM_MODEL_LENGTH = 256;
const BUILT_IN_MODEL_SLUGS_BY_PROVIDER: Record<ProviderKind, ReadonlySet<string>> = {
Expand All @@ -21,6 +21,9 @@ const AppSettingsSchema = Schema.Struct({
enableAssistantStreaming: Schema.Boolean.pipe(
Schema.withConstructorDefault(() => Option.some(false)),
),
newThreadUsesNewWorktree: Schema.Boolean.pipe(
Schema.withConstructorDefault(() => Option.some(false)),
),
customCodexModels: Schema.Array(Schema.String).pipe(
Schema.withConstructorDefault(() => Option.some([])),
),
Expand Down Expand Up @@ -174,7 +177,15 @@ function parsePersistedSettings(value: string | null): AppSettings {
}

try {
return normalizeAppSettings(Schema.decodeSync(Schema.fromJsonString(AppSettingsSchema))(value));
const parsed = JSON.parse(value);
const persistedSettings =
parsed && typeof parsed === "object"
? Schema.decodeSync(AppSettingsSchema)({
...DEFAULT_APP_SETTINGS,
...parsed,
})
: DEFAULT_APP_SETTINGS;
return normalizeAppSettings(persistedSettings);
} catch {
return DEFAULT_APP_SETTINGS;
}
Expand Down
170 changes: 161 additions & 9 deletions apps/web/src/components/ChatView.browser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -353,14 +353,14 @@ function createSnapshotWithLongProposedPlan(): OrchestrationReadModel {
};
}

function resolveWsRpc(tag: string): unknown {
if (tag === ORCHESTRATION_WS_METHODS.getSnapshot) {
function resolveWsRpc(request: WsRequestEnvelope["body"]): unknown {
if (request._tag === ORCHESTRATION_WS_METHODS.getSnapshot) {
return fixture.snapshot;
}
if (tag === WS_METHODS.serverGetConfig) {
if (request._tag === WS_METHODS.serverGetConfig) {
return fixture.serverConfig;
}
if (tag === WS_METHODS.gitListBranches) {
if (request._tag === WS_METHODS.gitListBranches) {
return {
isRepo: true,
hasOriginRemote: true,
Expand All @@ -374,7 +374,7 @@ function resolveWsRpc(tag: string): unknown {
],
};
}
if (tag === WS_METHODS.gitStatus) {
if (request._tag === WS_METHODS.gitStatus) {
return {
branch: "main",
hasWorkingTreeChanges: false,
Expand All @@ -389,7 +389,18 @@ function resolveWsRpc(tag: string): unknown {
pr: null,
};
}
if (tag === WS_METHODS.projectsSearchEntries) {
if (request._tag === WS_METHODS.gitCreateWorktree) {
return {
worktree: {
branch: typeof request.newBranch === "string" ? request.newBranch : "t3code/browser-test",
path:
typeof request.path === "string"
? request.path
: "/repo/project/.t3/worktrees/browser-test",
},
};
}
if (request._tag === WS_METHODS.projectsSearchEntries) {
return {
entries: [],
truncated: false,
Expand Down Expand Up @@ -417,13 +428,12 @@ const worker = setupWorker(
} catch {
return;
}
const method = request.body?._tag;
if (typeof method !== "string") return;
if (typeof request.body?._tag !== "string") return;
wsRequests.push(request.body);
client.send(
JSON.stringify({
id: request.id,
result: resolveWsRpc(method),
result: resolveWsRpc(request.body),
}),
);
});
Expand Down Expand Up @@ -526,6 +536,17 @@ async function waitForInteractionModeButton(
);
}

function findDispatchCommandRequest(commandType: string) {
return wsRequests.find(
(request) =>
request._tag === ORCHESTRATION_WS_METHODS.dispatchCommand &&
typeof request.command === "object" &&
request.command !== null &&
"type" in request.command &&
request.command.type === commandType,
);
}

async function waitForImagesToLoad(scope: ParentNode): Promise<void> {
const images = Array.from(scope.querySelectorAll("img"));
if (images.length === 0) {
Expand Down Expand Up @@ -1048,6 +1069,137 @@ describe("ChatView timeline estimator parity (full app)", () => {
}
});

it("reopens an existing draft thread without resetting its branch or mode", async () => {
useComposerDraftStore.setState({
draftThreadsByThreadId: {
[THREAD_ID]: {
projectId: PROJECT_ID,
createdAt: NOW_ISO,
runtimeMode: "full-access",
interactionMode: "default",
branch: "feature/existing-draft",
worktreePath: null,
envMode: "worktree",
},
},
projectDraftThreadIdByProjectId: {
[PROJECT_ID]: THREAD_ID,
},
});
const mounted = await mountChatView({
viewport: DEFAULT_VIEWPORT,
snapshot: createDraftOnlySnapshot(),
});

try {
const newThreadButton = page.getByTestId("new-thread-button");
await expect.element(newThreadButton).toBeInTheDocument();
await newThreadButton.click();

await waitForURL(
mounted.router,
(path) => path === `/${THREAD_ID}`,
"Existing draft thread should remain selected when reopening it.",
);

expect(useComposerDraftStore.getState().getDraftThread(THREAD_ID)).toMatchObject({
projectId: PROJECT_ID,
branch: "feature/existing-draft",
worktreePath: null,
envMode: "worktree",
});
} finally {
await mounted.cleanup();
}
});

it("creates the worktree on first send for a pending worktree draft", async () => {
useComposerDraftStore.setState({
draftThreadsByThreadId: {
[THREAD_ID]: {
projectId: PROJECT_ID,
createdAt: NOW_ISO,
runtimeMode: "full-access",
interactionMode: "default",
branch: "main",
worktreePath: null,
envMode: "worktree",
},
},
projectDraftThreadIdByProjectId: {
[PROJECT_ID]: THREAD_ID,
},
});
const mounted = await mountChatView({
viewport: DEFAULT_VIEWPORT,
snapshot: createDraftOnlySnapshot(),
});

try {
const composerEditor = page.getByTestId("composer-editor");
await composerEditor.fill("Ship the change");
await page.getByRole("button", { name: "Send message" }).click();

let worktreeNewBranch: string | null = null;
await vi.waitFor(
() => {
const worktreeRequest = wsRequests.find(
(request) => request._tag === WS_METHODS.gitCreateWorktree,
);
expect(worktreeRequest).toMatchObject({
_tag: WS_METHODS.gitCreateWorktree,
cwd: "/repo/project",
branch: "main",
});
worktreeNewBranch =
worktreeRequest && typeof worktreeRequest.newBranch === "string"
? worktreeRequest.newBranch
: null;
},
{ timeout: 8_000, interval: 16 },
);

await vi.waitFor(
() => {
const threadCreateRequest = findDispatchCommandRequest("thread.create");
expect(threadCreateRequest).toMatchObject({
_tag: ORCHESTRATION_WS_METHODS.dispatchCommand,
command: {
type: "thread.create",
threadId: THREAD_ID,
projectId: PROJECT_ID,
worktreePath: "/repo/project/.t3/worktrees/browser-test",
},
});
expect(
threadCreateRequest &&
typeof threadCreateRequest.command === "object" &&
threadCreateRequest.command !== null &&
"branch" in threadCreateRequest.command
? threadCreateRequest.command.branch
: null,
).toBe(worktreeNewBranch ?? "t3code/browser-test");
},
{ timeout: 8_000, interval: 16 },
);

expect(
wsRequests.findIndex((request) => request._tag === WS_METHODS.gitCreateWorktree),
).toBeLessThan(
wsRequests.findIndex(
(request) =>
request._tag === ORCHESTRATION_WS_METHODS.dispatchCommand &&
typeof request.command === "object" &&
request.command !== null &&
"type" in request.command &&
request.command.type === "thread.create",
),
);
} finally {
await mounted.cleanup();
}
});

it("keeps long proposed plans lightweight until the user expands them", async () => {
const mounted = await mountChatView({
viewport: DEFAULT_VIEWPORT,
Expand Down
Loading