Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

test(desktop): producer-truthful zip fixtures and merge/path properties (R1-M6)

M13b95b238fbc
diff --git a/desktop-app/ui/src/hooks/domain/__tests__/composerMergeZipProperties.test.tsx b/desktop-app/ui/src/hooks/domain/__tests__/composerMergeZipProperties.test.tsx
new file mode 100644
index 000000000..c4c831afd
--- /dev/null
+++ b/desktop-app/ui/src/hooks/domain/__tests__/composerMergeZipProperties.test.tsx
@@ -0,0 +1,139 @@
+// @vitest-environment jsdom
+/**
+ * R1-M6 — property/invariant coverage for the merge, dedupe, cap and path
+ * normalization rules the cancel-restore and folder-zip paths rely on. These
+ * complement the example tests: the examples pin behavior at the boundaries,
+ * the properties pin the invariants for arbitrary inputs.
+ */
+import { describe, expect, it } from 'vitest'
+import fc from 'fast-check'
+import { FILE_REFERENCE_MAX_COUNT } from '@clerum/gfs-interaction-policy'
+import { COMPOSER_MAX_IMAGE_ATTACHMENTS } from '@constants/attachments'
+import { GFS_ZIP_MAX_SEGMENT_NAME_BYTES, sanitizeZipSegment } from '@lib/gfsFolderZip'
+import { createZipWriter } from '@lib/zipWriter'
+import type { ComposerImageAttachment, ComposerReferenceAttachment } from '@/uiTypes'
+import {
+ mergeComposerImageAttachments,
+ mergeComposerReferenceAttachments,
+} from '../useComposerAttachments'
+
+const imageArbitrary = fc
+ .record({
+ id: fc.string({ minLength: 1, maxLength: 12 }),
+ dataBase64: fc.string({ minLength: 1, maxLength: 8 }),
+ sizeBytes: fc.nat(1000),
+ })
+ .map(record => ({
+ ...record,
+ name: `${record.id}.png`,
+ mimeType: 'image/png' as const,
+ previewDataUrl: `data:image/png;base64,${record.dataBase64}`,
+ }))
+
+const referenceArbitrary = fc
+ .record({
+ id: fc.string({ minLength: 1, maxLength: 12 }),
+ label: fc.string({ minLength: 0, maxLength: 12 }),
+ })
+ .map(record => ({
+ ...record,
+ type: 'plugin' as const,
+ namespace: 'ns',
+ name: record.id,
+ }))
+
+describe('composer merge invariants (R1-M6)', () => {
+ it('image merge: cap, count conservation, and existing-set stability', () => {
+ fc.assert(
+ fc.property(
+ fc.array(imageArbitrary, { maxLength: 30 }),
+ fc.array(imageArbitrary, { maxLength: 30 }),
+ (existing, incoming) => {
+ const outcome = mergeComposerImageAttachments(
+ existing as ComposerImageAttachment[],
+ incoming as ComposerImageAttachment[]
+ )
+ expect(outcome.next.length).toBeLessThanOrEqual(COMPOSER_MAX_IMAGE_ATTACHMENTS)
+ expect(outcome.kept + outcome.duplicates + outcome.dropped).toBe(incoming.length)
+ // The composer never loses what it already held.
+ expect(outcome.next.slice(0, existing.length)).toEqual(existing)
+ // Duplicates never consume slots: keeping room is consistent with counts.
+ expect(outcome.kept).toBe(outcome.next.length - existing.length)
+ }
+ ),
+ { numRuns: 60 }
+ )
+ })
+
+ it('reference merge: file cap, count conservation, and id uniqueness', () => {
+ fc.assert(
+ fc.property(
+ fc.array(referenceArbitrary, { maxLength: 25 }),
+ fc.array(referenceArbitrary, { maxLength: 25 }),
+ (existing, incoming) => {
+ const outcome = mergeComposerReferenceAttachments(
+ existing as ComposerReferenceAttachment[],
+ incoming as ComposerReferenceAttachment[]
+ )
+ expect(outcome.next.length).toBeLessThanOrEqual(FILE_REFERENCE_MAX_COUNT)
+ expect(outcome.kept + outcome.duplicates + outcome.dropped).toBe(incoming.length)
+ const ids = new Set(outcome.next.map(item => item.id))
+ expect(ids.size).toBe(outcome.next.length)
+ }
+ ),
+ { numRuns: 60 }
+ )
+ })
+})
+
+describe('zip path normalization invariants (R1-M6)', () => {
+ it('sanitized segments never carry separators, reserved chars, or excess length', () => {
+ fc.assert(
+ fc.property(fc.string({ maxLength: 2000 }), raw => {
+ const segment = sanitizeZipSegment(raw)
+ expect(segment.length).toBeGreaterThan(0)
+ expect(segment).not.toMatch(/[/\\:*?"<>|\u0000]/)
+ expect(new TextEncoder().encode(segment).length).toBeLessThanOrEqual(
+ GFS_ZIP_MAX_SEGMENT_NAME_BYTES
+ )
+ expect(segment).not.toBe('.')
+ expect(segment).not.toBe('..')
+ }),
+ { numRuns: 200 }
+ )
+ })
+
+ it('built archives never contain case-folded duplicate entry names', () => {
+ fc.assert(
+ fc.property(
+ fc.array(fc.string({ minLength: 1, maxLength: 24 }), { maxLength: 30 }),
+ names => {
+ const writer = createZipWriter()
+ for (const [index, name] of names.entries()) writer.addFile(name, new Uint8Array([index]))
+ const written = archiveEntryNames(writer.build())
+ const folded = new Set(written.map(name => name.toLowerCase()))
+ expect(folded.size).toBe(written.length)
+ expect(written).toHaveLength(names.length)
+ }
+ ),
+ { numRuns: 60 }
+ )
+ })
+})
+
+/** Minimal central-directory reader (name column only). */
+function archiveEntryNames(archive: Uint8Array): string[] {
+ const view = new DataView(archive.buffer, archive.byteOffset, archive.byteLength)
+ const decoder = new TextDecoder()
+ const eocdOffset = archive.length - 22
+ const entryCount = view.getUint16(eocdOffset + 10, true)
+ const centralDirectoryOffset = view.getUint32(eocdOffset + 16, true)
+ const names: string[] = []
+ let cursor = centralDirectoryOffset
+ for (let index = 0; index < entryCount; index += 1) {
+ const nameLength = view.getUint16(cursor + 28, true)
+ names.push(decoder.decode(archive.subarray(cursor + 46, cursor + 46 + nameLength)))
+ cursor += 46 + nameLength
+ }
+ return names
+}
diff --git a/desktop-app/ui/src/lib/gfsFolderZip.ts b/desktop-app/ui/src/lib/gfsFolderZip.ts
index 3ffea63f6..e0995180d 100644
--- a/desktop-app/ui/src/lib/gfsFolderZip.ts
+++ b/desktop-app/ui/src/lib/gfsFolderZip.ts
@@ -230,7 +230,8 @@ function truncateToUtf8Bytes(name: string, maxBytes: number): string {
* truncate. Traversal is impossible regardless: separators are gone before the
* segment is used.
*/
-function sanitizeZipSegment(name: string): string {
+/** Exported for property-based invariant tests (R1-M6). */
+export function sanitizeZipSegment(name: string): string {
const cleaned = truncateToUtf8Bytes(
name
.replace(/[/\\:*?"<>|\u0000]/g, '_')
diff --git a/desktop-app/ui/src/pages/__tests__/FilesPage.test.tsx b/desktop-app/ui/src/pages/__tests__/FilesPage.test.tsx
index 43e3c6505..8bf85f978 100644
--- a/desktop-app/ui/src/pages/__tests__/FilesPage.test.tsx
+++ b/desktop-app/ui/src/pages/__tests__/FilesPage.test.tsx
@@ -6,6 +6,7 @@ import { act, cleanup, fireEvent, render, screen, waitFor, within } from '@testi
import { GFS_FILE_UPLOAD_PROTOCOL_MAX_BYTES } from '@constants/gfsFileUpload'
import { desktopQueryKeys } from '@hooks/domain/queryKeys'
import type { GfsCrumb } from '@hooks/domain/useGfsBrowserController'
+import { childView, listChildrenPage } from '@/gfs/__fixtures__/gfsProducerFixtures'
import type { Tone } from '@/uiTypes'
import { FilesPage } from '../FilesPage'
@@ -3295,36 +3296,21 @@ describe('FilesPage', () => {
it('downloads a folder recursively as one zip with progress and a skip notice (BUG-175)', async () => {
vi.useFakeTimers()
- const listChildren = vi.fn(async () => ({
- items: [
- {
- resourceId: 'doc-1',
- rid: 'doc-1',
- gfsUri: 'gfs://main/doc-1',
- drive: 'main',
+ // Producer-truthful wire fixtures: the children flow through the real
+ // GfsClient + structuredClone, not a hand-rolled .d.ts mirror (R1-M6).
+ const listChildren = vi.fn(async () =>
+ listChildrenPage([
+ childView('doc-1', 'plan.md', 'file', {
parentResourceId: 'folder-1',
- name: 'plan.md',
- kind: 'file' as const,
path: '/Assets/plan.md',
- version: 0,
- bytes: 4,
- },
- {
- resourceId: 'hidden-1',
- rid: 'hidden-1',
- gfsUri: 'gfs://main/hidden-1',
- drive: 'main',
+ }),
+ childView('hidden-1', 'secret.txt', 'file', {
parentResourceId: 'folder-1',
- name: 'secret.txt',
- kind: 'file' as const,
path: '/Assets/secret.txt',
- version: 0,
- bytes: 4,
readable: false,
- },
- ],
- nextCursor: null,
- }))
+ }),
+ ])
+ )
const download = vi.fn(async () => ({ bytes: new Uint8Array([1, 2, 3, 4]).buffer }))
const pushToast = vi.fn()
const createObjectURL = vi.fn(() => 'blob:folder-zip')
@@ -3378,8 +3364,13 @@ describe('FilesPage', () => {
})
expect(screen.queryByTestId('gfs-zip-progress')).toBeNull()
- expect(listChildren).toHaveBeenCalledWith('folder-1', 'main', undefined)
- expect(download).toHaveBeenCalledWith('gfs://main/doc-1', { maxBytes: 512 * 1024 * 1024 })
+ expect(listChildren).toHaveBeenCalledWith('folder-1', 'main', undefined, {
+ signal: expect.any(AbortSignal),
+ })
+ expect(download).toHaveBeenCalledWith('gfs://main/doc-1', {
+ maxBytes: 512 * 1024 * 1024,
+ signal: expect.any(AbortSignal),
+ })
expect(createObjectURL).toHaveBeenCalledWith(expect.any(Blob))
expect(click).toHaveBeenCalled()
const savedName = (click.mock.instances[0] as HTMLAnchorElement | undefined)?.download
@@ -3394,23 +3385,15 @@ describe('FilesPage', () => {
it('refuses a folder zip over the size limit with a clear message and saves nothing', async () => {
vi.useFakeTimers()
- const listChildren = vi.fn(async () => ({
- items: [
- {
- resourceId: 'huge-1',
- rid: 'huge-1',
- gfsUri: 'gfs://main/huge-1',
- drive: 'main',
+ const listChildren = vi.fn(async () =>
+ listChildrenPage([
+ childView('huge-1', 'huge.bin', 'file', {
parentResourceId: 'folder-1',
- name: 'huge.bin',
- kind: 'file' as const,
path: '/Assets/huge.bin',
- version: 0,
bytes: 1024 * 1024 * 1024 + 1,
- },
- ],
- nextCursor: null,
- }))
+ }),
+ ])
+ )
const download = vi.fn(async () => ({ bytes: new ArrayBuffer(0) }))
const pushToast = vi.fn()
const click = vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(() => {})
@@ -3504,7 +3487,9 @@ describe('FilesPage', () => {
})
expect(screen.queryByTestId('gfs-zip-progress')).toBeNull()
- expect(listChildren).toHaveBeenCalledWith('folder-1', 'main', undefined)
+ expect(listChildren).toHaveBeenCalledWith('folder-1', 'main', undefined, {
+ signal: expect.any(AbortSignal),
+ })
expect(download).not.toHaveBeenCalled()
expect(click).not.toHaveBeenCalled()
expect(pushToast).toHaveBeenCalledWith('"Empty" has no downloadable files.', 'info')
@@ -3512,23 +3497,14 @@ describe('FilesPage', () => {
it('stops a running folder zip from the progress strip without saving (M3)', async () => {
vi.useFakeTimers()
- const listChildren = vi.fn(async () => ({
- items: [
- {
- resourceId: 'doc-1',
- rid: 'doc-1',
- gfsUri: 'gfs://main/doc-1',
- drive: 'main',
+ const listChildren = vi.fn(async () =>
+ listChildrenPage([
+ childView('doc-1', 'plan.md', 'file', {
parentResourceId: 'folder-1',
- name: 'plan.md',
- kind: 'file' as const,
path: '/Assets/plan.md',
- version: 0,
- bytes: 4,
- },
- ],
- nextCursor: null,
- }))
+ }),
+ ])
+ )
const download = vi.fn(async () => ({ bytes: new Uint8Array([1, 2, 3, 4]).buffer }))
const pushToast = vi.fn()
const click = vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(() => {})
@@ -3599,13 +3575,13 @@ describe('FilesPage', () => {
// Real timers: the first walk parks on the in-flight listing itself, so
// only the abort race can end it; the second walk's first request is
// immediate on its own fresh throttle.
- let settleFirst!: (value: { items: unknown[]; nextCursor: null }) => void
+ let settleFirst!: (value: Awaited<ReturnType<typeof listChildrenPage>>) => void
const listChildren = vi.fn(() => {
if (listChildren.mock.calls.length === 1)
return new Promise(resolve => {
settleFirst = resolve
})
- return Promise.resolve({ items: [], nextCursor: null })
+ return listChildrenPage([])
})
const pushToast = vi.fn()
Object.defineProperty(window, 'clerum', {
@@ -3777,7 +3753,9 @@ describe('FilesPage', () => {
})
expect(listChildren).toHaveBeenCalledTimes(1)
- expect(listChildren).toHaveBeenCalledWith('folder-1', 'main', undefined)
+ expect(listChildren).toHaveBeenCalledWith('folder-1', 'main', undefined, {
+ signal: expect.any(AbortSignal),
+ })
expect(listChildren).not.toHaveBeenCalledWith('folder-p', 'main', undefined)
})