- replace `read*` draft selectors with `getDraftThread`/`getDraftThreadByProjectId` - centralize persisted localStorage parsing/caching in `composerDraftStore` - make ChatView fallback read persisted attachments from in-memory store state
245 lines
8.0 KiB
TypeScript
245 lines
8.0 KiB
TypeScript
import { ProjectId, ThreadId } from "@t3tools/contracts";
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
import { type ComposerImageAttachment, useComposerDraftStore } from "./composerDraftStore";
|
|
|
|
function makeImage(input: {
|
|
id: string;
|
|
previewUrl: string;
|
|
name?: string;
|
|
mimeType?: string;
|
|
sizeBytes?: number;
|
|
lastModified?: number;
|
|
}): ComposerImageAttachment {
|
|
const name = input.name ?? "image.png";
|
|
const mimeType = input.mimeType ?? "image/png";
|
|
const sizeBytes = input.sizeBytes ?? 4;
|
|
const lastModified = input.lastModified ?? 1_700_000_000_000;
|
|
const file = new File([new Uint8Array(sizeBytes).fill(1)], name, {
|
|
type: mimeType,
|
|
lastModified,
|
|
});
|
|
return {
|
|
type: "image",
|
|
id: input.id,
|
|
name,
|
|
mimeType,
|
|
sizeBytes: file.size,
|
|
previewUrl: input.previewUrl,
|
|
file,
|
|
};
|
|
}
|
|
|
|
describe("composerDraftStore addImages", () => {
|
|
const threadId = ThreadId.makeUnsafe("thread-dedupe");
|
|
let originalRevokeObjectUrl: typeof URL.revokeObjectURL;
|
|
let revokeSpy: ReturnType<typeof vi.fn<(url: string) => void>>;
|
|
|
|
beforeEach(() => {
|
|
useComposerDraftStore.setState({
|
|
draftsByThreadId: {},
|
|
draftThreadsByThreadId: {},
|
|
projectDraftThreadIdByProjectId: {},
|
|
});
|
|
originalRevokeObjectUrl = URL.revokeObjectURL;
|
|
revokeSpy = vi.fn();
|
|
URL.revokeObjectURL = revokeSpy;
|
|
});
|
|
|
|
afterEach(() => {
|
|
URL.revokeObjectURL = originalRevokeObjectUrl;
|
|
});
|
|
|
|
it("deduplicates identical images in one batch by file signature", () => {
|
|
const first = makeImage({
|
|
id: "img-1",
|
|
previewUrl: "blob:first",
|
|
name: "same.png",
|
|
mimeType: "image/png",
|
|
sizeBytes: 12,
|
|
lastModified: 12345,
|
|
});
|
|
const duplicate = makeImage({
|
|
id: "img-2",
|
|
previewUrl: "blob:duplicate",
|
|
name: "same.png",
|
|
mimeType: "image/png",
|
|
sizeBytes: 12,
|
|
lastModified: 12345,
|
|
});
|
|
|
|
useComposerDraftStore.getState().addImages(threadId, [first, duplicate]);
|
|
|
|
const draft = useComposerDraftStore.getState().draftsByThreadId[threadId];
|
|
expect(draft?.images.map((image) => image.id)).toEqual(["img-1"]);
|
|
expect(revokeSpy).toHaveBeenCalledWith("blob:duplicate");
|
|
});
|
|
|
|
it("deduplicates against existing images across calls by file signature", () => {
|
|
const first = makeImage({
|
|
id: "img-a",
|
|
previewUrl: "blob:a",
|
|
name: "same.png",
|
|
mimeType: "image/png",
|
|
sizeBytes: 9,
|
|
lastModified: 777,
|
|
});
|
|
const duplicateLater = makeImage({
|
|
id: "img-b",
|
|
previewUrl: "blob:b",
|
|
name: "same.png",
|
|
mimeType: "image/png",
|
|
sizeBytes: 9,
|
|
lastModified: 999,
|
|
});
|
|
|
|
useComposerDraftStore.getState().addImage(threadId, first);
|
|
useComposerDraftStore.getState().addImage(threadId, duplicateLater);
|
|
|
|
const draft = useComposerDraftStore.getState().draftsByThreadId[threadId];
|
|
expect(draft?.images.map((image) => image.id)).toEqual(["img-a"]);
|
|
expect(revokeSpy).toHaveBeenCalledWith("blob:b");
|
|
});
|
|
|
|
it("does not revoke blob URLs that are still used by an accepted duplicate image", () => {
|
|
const first = makeImage({
|
|
id: "img-shared",
|
|
previewUrl: "blob:shared",
|
|
});
|
|
const duplicateSameUrl = makeImage({
|
|
id: "img-shared",
|
|
previewUrl: "blob:shared",
|
|
});
|
|
|
|
useComposerDraftStore.getState().addImages(threadId, [first, duplicateSameUrl]);
|
|
|
|
const draft = useComposerDraftStore.getState().draftsByThreadId[threadId];
|
|
expect(draft?.images.map((image) => image.id)).toEqual(["img-shared"]);
|
|
expect(revokeSpy).not.toHaveBeenCalledWith("blob:shared");
|
|
});
|
|
});
|
|
|
|
describe("composerDraftStore clearComposerContent", () => {
|
|
const threadId = ThreadId.makeUnsafe("thread-clear");
|
|
let originalRevokeObjectUrl: typeof URL.revokeObjectURL;
|
|
let revokeSpy: ReturnType<typeof vi.fn<(url: string) => void>>;
|
|
|
|
beforeEach(() => {
|
|
useComposerDraftStore.setState({
|
|
draftsByThreadId: {},
|
|
draftThreadsByThreadId: {},
|
|
projectDraftThreadIdByProjectId: {},
|
|
});
|
|
originalRevokeObjectUrl = URL.revokeObjectURL;
|
|
revokeSpy = vi.fn();
|
|
URL.revokeObjectURL = revokeSpy;
|
|
});
|
|
|
|
afterEach(() => {
|
|
URL.revokeObjectURL = originalRevokeObjectUrl;
|
|
});
|
|
|
|
it("does not revoke blob preview URLs when clearing composer content", () => {
|
|
const first = makeImage({
|
|
id: "img-optimistic",
|
|
previewUrl: "blob:optimistic",
|
|
});
|
|
useComposerDraftStore.getState().addImage(threadId, first);
|
|
|
|
useComposerDraftStore.getState().clearComposerContent(threadId);
|
|
|
|
const draft = useComposerDraftStore.getState().draftsByThreadId[threadId];
|
|
expect(draft).toBeUndefined();
|
|
expect(revokeSpy).not.toHaveBeenCalledWith("blob:optimistic");
|
|
});
|
|
});
|
|
|
|
describe("composerDraftStore project draft thread mapping", () => {
|
|
const projectId = ProjectId.makeUnsafe("project-a");
|
|
const threadId = ThreadId.makeUnsafe("thread-a");
|
|
const otherThreadId = ThreadId.makeUnsafe("thread-b");
|
|
|
|
beforeEach(() => {
|
|
useComposerDraftStore.setState({
|
|
draftsByThreadId: {},
|
|
draftThreadsByThreadId: {},
|
|
projectDraftThreadIdByProjectId: {},
|
|
});
|
|
});
|
|
|
|
it("stores and reads project draft thread ids via actions", () => {
|
|
const store = useComposerDraftStore.getState();
|
|
expect(store.getDraftThreadByProjectId(projectId)).toBeNull();
|
|
expect(store.getDraftThread(threadId)).toBeNull();
|
|
|
|
store.setProjectDraftThreadId(projectId, threadId, {
|
|
branch: "feature/test",
|
|
worktreePath: "/tmp/worktree-test",
|
|
createdAt: "2026-01-01T00:00:00.000Z",
|
|
});
|
|
expect(useComposerDraftStore.getState().getDraftThreadByProjectId(projectId)).toEqual({
|
|
threadId,
|
|
projectId,
|
|
branch: "feature/test",
|
|
worktreePath: "/tmp/worktree-test",
|
|
createdAt: "2026-01-01T00:00:00.000Z",
|
|
});
|
|
expect(useComposerDraftStore.getState().getDraftThread(threadId)).toEqual({
|
|
projectId,
|
|
branch: "feature/test",
|
|
worktreePath: "/tmp/worktree-test",
|
|
createdAt: "2026-01-01T00:00:00.000Z",
|
|
});
|
|
});
|
|
|
|
it("clears only matching project draft mapping entries", () => {
|
|
const store = useComposerDraftStore.getState();
|
|
store.setProjectDraftThreadId(projectId, threadId);
|
|
|
|
store.clearProjectDraftThreadById(projectId, otherThreadId);
|
|
expect(useComposerDraftStore.getState().getDraftThreadByProjectId(projectId)?.threadId).toBe(
|
|
threadId,
|
|
);
|
|
|
|
store.clearProjectDraftThreadById(projectId, threadId);
|
|
expect(useComposerDraftStore.getState().getDraftThreadByProjectId(projectId)).toBeNull();
|
|
expect(useComposerDraftStore.getState().getDraftThread(threadId)).toBeNull();
|
|
});
|
|
|
|
it("clears project draft mapping by project id", () => {
|
|
const store = useComposerDraftStore.getState();
|
|
store.setProjectDraftThreadId(projectId, threadId);
|
|
store.clearProjectDraftThreadId(projectId);
|
|
expect(useComposerDraftStore.getState().getDraftThreadByProjectId(projectId)).toBeNull();
|
|
expect(useComposerDraftStore.getState().getDraftThread(threadId)).toBeNull();
|
|
});
|
|
|
|
it("clears draft registration independently", () => {
|
|
const store = useComposerDraftStore.getState();
|
|
store.setProjectDraftThreadId(projectId, threadId);
|
|
store.clearDraftThread(threadId);
|
|
expect(useComposerDraftStore.getState().getDraftThreadByProjectId(projectId)).toBeNull();
|
|
expect(useComposerDraftStore.getState().getDraftThread(threadId)).toBeNull();
|
|
});
|
|
|
|
it("updates branch context on an existing draft thread", () => {
|
|
const store = useComposerDraftStore.getState();
|
|
store.setProjectDraftThreadId(projectId, threadId, {
|
|
branch: "main",
|
|
worktreePath: null,
|
|
});
|
|
store.setDraftThreadContext(threadId, {
|
|
branch: "feature/next",
|
|
worktreePath: "/tmp/feature-next",
|
|
});
|
|
expect(useComposerDraftStore.getState().getDraftThreadByProjectId(projectId)?.threadId).toBe(
|
|
threadId,
|
|
);
|
|
expect(useComposerDraftStore.getState().getDraftThread(threadId)).toMatchObject({
|
|
projectId,
|
|
branch: "feature/next",
|
|
worktreePath: "/tmp/feature-next",
|
|
});
|
|
});
|
|
});
|