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
54 changes: 39 additions & 15 deletions apps/web/src/composerDraftStore.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as Schema from "effect/Schema";
import { ProjectId, ThreadId } from "@t3tools/contracts";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";

Expand All @@ -7,7 +6,7 @@
type ComposerImageAttachment,
useComposerDraftStore,
} from "./composerDraftStore";
import { removeLocalStorageItem, setLocalStorageItem } from "./hooks/useLocalStorage";
import { removeLocalStorageItem } from "./hooks/useLocalStorage";
import {
INLINE_TERMINAL_CONTEXT_PLACEHOLDER,
insertInlineTerminalContextPlaceholder,
Expand Down Expand Up @@ -204,26 +203,49 @@
removeLocalStorageItem(COMPOSER_DRAFT_STORAGE_KEY);
});

it("treats malformed persisted draft storage as empty", async () => {
it("keeps attachments persisted after flushing the pending draft write", async () => {
const image = makeImage({
id: "img-persisted",
previewUrl: "blob:persisted",
});
useComposerDraftStore.getState().addImage(threadId, image);
setLocalStorageItem(
COMPOSER_DRAFT_STORAGE_KEY,

useComposerDraftStore.getState().syncPersistedAttachments(threadId, [
{
version: 2,
state: {
draftsByThreadId: {
[threadId]: {
attachments: "not-an-array",
},
},
},
id: image.id,
name: image.name,
mimeType: image.mimeType,
sizeBytes: image.sizeBytes,
dataUrl: image.previewUrl,
},
Schema.Unknown,
);
]);
await Promise.resolve();

expect(
useComposerDraftStore.getState().draftsByThreadId[threadId]?.persistedAttachments,
).toEqual([

Check failure on line 226 in apps/web/src/composerDraftStore.test.ts

View workflow job for this annotation

GitHub Actions / Format, Lint, Typecheck, Test, Browser Test, Build

src/composerDraftStore.test.ts > composerDraftStore syncPersistedAttachments > keeps attachments persisted after flushing the pending draft write

AssertionError: expected [] to deeply equal [ { id: 'img-persisted', …(4) } ] - Expected + Received - [ - { - "dataUrl": "blob:persisted", - "id": "img-persisted", - "mimeType": "image/png", - "name": "image.png", - "sizeBytes": 4, - }, - ] + [] ❯ src/composerDraftStore.test.ts:226:7
{
id: image.id,
name: image.name,
mimeType: image.mimeType,
sizeBytes: image.sizeBytes,
dataUrl: image.previewUrl,
},
]);
expect(
useComposerDraftStore.getState().draftsByThreadId[threadId]?.nonPersistedImageIds,
).toEqual([]);
});

it("marks attachments non-persisted when flushing draft storage fails", async () => {
const image = makeImage({
id: "img-persisted",
previewUrl: "blob:persisted",
});
useComposerDraftStore.getState().addImage(threadId, image);
const setItemSpy = vi.spyOn(Storage.prototype, "setItem").mockImplementation(() => {

Check failure on line 246 in apps/web/src/composerDraftStore.test.ts

View workflow job for this annotation

GitHub Actions / Format, Lint, Typecheck, Test, Browser Test, Build

src/composerDraftStore.test.ts > composerDraftStore syncPersistedAttachments > marks attachments non-persisted when flushing draft storage fails

ReferenceError: Storage is not defined ❯ src/composerDraftStore.test.ts:246:33
throw new Error("Quota exceeded");
});

useComposerDraftStore.getState().syncPersistedAttachments(threadId, [
{
Expand All @@ -242,6 +264,8 @@
expect(
useComposerDraftStore.getState().draftsByThreadId[threadId]?.nonPersistedImageIds,
).toEqual([image.id]);

setItemSpy.mockRestore();
});
});

Expand Down
14 changes: 13 additions & 1 deletion apps/web/src/composerDraftStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,15 @@ function readPersistedAttachmentIdsFromStorage(threadId: ThreadId): string[] {
}
}

function flushComposerDraftStorage(): boolean {
try {
composerDebouncedStorage.flush();
return true;
} catch {
return false;
}
}

function hydreatePersistedComposerImageAttachment(
attachment: PersistedComposerImageAttachment,
): File | null {
Expand Down Expand Up @@ -1662,7 +1671,10 @@ export const useComposerDraftStore = create<ComposerDraftStoreState>()(
return { draftsByThreadId: nextDraftsByThreadId };
});
Promise.resolve().then(() => {
const persistedIdSet = new Set(readPersistedAttachmentIdsFromStorage(threadId));
const didFlushPersistedDraft = flushComposerDraftStorage();
const persistedIdSet = didFlushPersistedDraft
? new Set(readPersistedAttachmentIdsFromStorage(threadId))
: new Set<string>();
set((state) => {
const current = state.draftsByThreadId[threadId];
if (!current) {
Expand Down
Loading