Skip to content

Commit 41a3b55

Browse files
committed
test(files): narrow recorded database insert values
1 parent 3bed99b commit 41a3b55

10 files changed

Lines changed: 59 additions & 112 deletions

File tree

‎apps/sim/executor/handlers/agent/agent-handler.test.ts‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
workspaceFileSecretProvenanceMock,
1919
workspaceFileSecretProvenanceMockFns,
2020
} from '@sim/testing/mocks/workspace-file-secret-provenance.mock'
21+
import { toArray, toRecord } from '@sim/utils/object'
2122
import {
2223
afterAll,
2324
afterEach,
@@ -604,7 +605,7 @@ describe('AgentBlockHandler', () => {
604605
const original = structuredClone(inputs)
605606
await handler.execute({ ...mockContext, executionId: 'exec-1' }, mockBlock, inputs)
606607
const stored = dbChainMockFns.values.mock.calls
607-
.map(([row]) => row)
608+
.map(([row]) => toRecord(row))
608609
.find((row) => Array.isArray(row.data) && row.data[0]?.role === 'user')?.data as Message[]
609610
expect(stored).toBeDefined()
610611
expect(stored[0].files).toEqual([
@@ -672,9 +673,11 @@ describe('AgentBlockHandler', () => {
672673
files: [file],
673674
})
674675
expect(mockExecuteProviderRequest.mock.calls[0][1].messages[0].files).toHaveLength(1)
675-
expect(dbChainMockFns.values.mock.calls.some(([row]) => row.data?.[0]?.role === 'user')).toBe(
676-
false
677-
)
676+
expect(
677+
dbChainMockFns.values.mock.calls.some(
678+
([row]) => toRecord(toArray(toRecord(row).data)[0]).role === 'user'
679+
)
680+
).toBe(false)
678681
})
679682
})
680683

@@ -842,7 +845,7 @@ describe('AgentBlockHandler', () => {
842845
}
843846

844847
const memoryWrites = dbChainMockFns.values.mock.calls
845-
.map(([row]) => row)
848+
.map(([row]) => toRecord(row))
846849
.filter((row) => Array.isArray(row.data))
847850
expect(memoryWrites.map((row) => row.data)).toEqual([
848851
[currentUserMessage],

‎apps/sim/lib/core/rate-limiter/provider-admission.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { rateLimitBucket } from '@sim/db/schema'
12
import { dbChainMockFns, resetDbChainMock } from '@sim/testing'
23
import { resetEnvMock, setEnv } from '@sim/testing/mocks/env.mock'
34
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
@@ -168,7 +169,7 @@ describe('provider admission', () => {
168169
}
169170
let stored: { key: string; tokens: string; lastRefillAt: Date } | undefined
170171
dbChainMockFns.values.mockImplementation((row) => {
171-
stored ??= { ...row }
172+
stored ??= { ...(row as typeof rateLimitBucket.$inferSelect) }
172173
return { onConflictDoNothing: vi.fn().mockResolvedValue(undefined) }
173174
})
174175
dbChainMockFns.limit.mockImplementation(async () => [stored])

‎apps/sim/lib/knowledge/documents/document-processing-source.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
workspaceFileSecretProvenanceMock,
2323
workspaceFileSecretProvenanceMockFns,
2424
} from '@sim/testing/mocks/workspace-file-secret-provenance.mock'
25+
import { toRecord } from '@sim/utils/object'
2526
import { tasks } from '@trigger.dev/sdk'
2627
import { DrizzleQueryError } from 'drizzle-orm/errors'
2728
import { afterAll, afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
@@ -1149,7 +1150,7 @@ describe('in-process quota continuation dispatch', () => {
11491150
})
11501151
)
11511152

1152-
const deferredUntil = dbChainMockFns.values.mock.calls[0][0].availableAt as Date
1153+
const deferredUntil = toRecord(dbChainMockFns.values.mock.calls[0][0]).availableAt as Date
11531154
expect(deferredUntil.getTime()).toBeGreaterThanOrEqual(1_000 + 5 * 60 * 1000 * 0.8)
11541155
expect(deferredUntil.getTime()).toBeLessThanOrEqual(1_000 + 5 * 60 * 1000 * 1.2)
11551156

‎apps/sim/lib/knowledge/documents/storage-cleanup.test.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
uploadsMetadataMock,
66
uploadsMetadataMockFns,
77
} from '@sim/testing/mocks/uploads-metadata.mock'
8+
import { toArray } from '@sim/utils/object'
89
import { beforeEach, describe, expect, it, vi } from 'vitest'
910
import type { OutboxEventContext } from '@/lib/core/outbox/service'
1011

@@ -94,7 +95,9 @@ describe('durable knowledge storage cleanup', () => {
9495
'request-1'
9596
)
9697
expect(mockGetBindings.mock.calls.map(([keys]) => keys.length)).toEqual([100, 100, 51])
97-
expect(dbChainMockFns.values.mock.calls.map(([rows]) => rows.length)).toEqual([100, 100, 51])
98+
expect(dbChainMockFns.values.mock.calls.map(([rows]) => toArray(rows).length)).toEqual([
99+
100, 100, 51,
100+
])
98101
})
99102

100103
it('propagates persistence failures before the parent transaction can commit', async () => {

‎apps/sim/lib/memory/message-provenance.test.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import {
1010
workspaceContextMock,
1111
workspaceContextMockFns,
1212
} from '@sim/testing/mocks/workspace-context.mock'
13-
import { beforeEach, describe, expect, it, vi } from 'vitest'
13+
import { toArray, toRecord } from '@sim/utils/object'
14+
import { assert, beforeEach, describe, expect, it, vi } from 'vitest'
1415

1516
vi.mock('@/lib/core/security/encryption', () => encryptionMock)
1617
vi.mock('@/lib/logs/execution/pii-redaction', () => piiRedactionMock)
@@ -160,7 +161,7 @@ describe('memory message provenance', () => {
160161
})
161162

162163
const sidecar = dbChainMockFns.values.mock.calls
163-
.map(([value]) => value)
164+
.map(([value]) => toRecord(value))
164165
.find((value) => value.memoryId === 'memory-1')
165166
expect(sidecar).toMatchObject({
166167
status: 'exact',
@@ -169,7 +170,8 @@ describe('memory message provenance', () => {
169170
expect(dbChainMockFns.execute.mock.invocationCallOrder[0]).toBeLessThan(
170171
dbChainMockFns.select.mock.invocationCallOrder[0]
171172
)
172-
queueStoredMemory(data, sidecar.entries)
173+
assert.isDefined(sidecar)
174+
queueStoredMemory(data, toArray<DurableSecretProvenanceEntry>(sidecar.entries))
173175
const result = await new Memory().fetchMemoryMessages(executionContext(), INPUTS)
174176
expect(result[0].content).toBe('{{TOKEN}}')
175177
expect(loggedErrors()).toEqual([])

‎apps/sim/lib/mothership/agent-cli/file-round-trip-provenance.test.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { tmpdir } from 'node:os'
55
import { join } from 'node:path'
66
import { Readable } from 'node:stream'
77
import { promisify } from 'node:util'
8+
import type { StoredWorkspaceFileSecretProvenanceEntry } from '@sim/db/schema'
89
import { copilotChats, workspace, workspaceFiles } from '@sim/db/schema'
910
import { dbChainMockFns, queueTableRows, resetDbChainMock } from '@sim/testing'
1011
import { authBanMock } from '@sim/testing/mocks/auth-ban.mock'
@@ -28,7 +29,8 @@ import {
2829
workspaceFileFoldersMock,
2930
workspaceFileFoldersMockFns,
3031
} from '@sim/testing/mocks/workspace-file-folders.mock'
31-
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
32+
import { toArray, toRecord } from '@sim/utils/object'
33+
import { afterEach, assert, beforeEach, describe, expect, it, vi } from 'vitest'
3234

3335
const hoisted = vi.hoisted(() => ({
3436
find: vi.fn(),
@@ -310,12 +312,16 @@ beforeEach(async () => {
310312
})
311313
expect(result.created).toBe(true)
312314
const sidecar = dbChainMockFns.values.mock.calls
313-
.map(([value]) => value)
315+
.map(([value]) => toRecord(value))
314316
.find((value) => value.fileId === PUBLISHED.id && 'entries' in value)
315317
expect(sidecar).toMatchObject({ contentUpdatedAt: REVISION })
318+
assert.isDefined(sidecar)
316319
storedProvenance =
317320
sidecar.status === 'exact'
318-
? { status: 'exact', entries: sidecar.entries }
321+
? {
322+
status: 'exact',
323+
entries: toArray<StoredWorkspaceFileSecretProvenanceEntry>(sidecar.entries),
324+
}
319325
: { status: 'unknown' }
320326
registered = true
321327
queueTableRows(workspaceFiles, [PUBLISHED])

‎apps/sim/lib/secrets/usage/record.test.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { dbChainMockFns, resetDbChainMock } from '@sim/testing'
22
import { flushMacrotask } from '@sim/testing/helpers/async'
3+
import { toArray } from '@sim/utils/object'
34
import { beforeEach, describe, expect, it, vi } from 'vitest'
45
import { recordSecretUsage } from '@/lib/secrets/usage/record'
56

@@ -28,7 +29,7 @@ describe('recordSecretUsage', () => {
2829
await flushMacrotask()
2930

3031
expect(dbChainMockFns.insert).toHaveBeenCalledTimes(1)
31-
const rows = dbChainMockFns.values.mock.calls[0]?.[0]
32+
const rows = toArray(dbChainMockFns.values.mock.calls[0]?.[0])
3233
expect(rows).toHaveLength(2)
3334
expect(rows[0]).toMatchObject({
3435
workspaceId: 'workspace-1',
@@ -71,7 +72,9 @@ describe('recordSecretUsage', () => {
7172
}
7273
await flushMacrotask()
7374

74-
expect(dbChainMockFns.values.mock.calls[0]?.[0][0]).toMatchObject({ usageDate: '2026-03-14' })
75+
expect(toArray(dbChainMockFns.values.mock.calls[0]?.[0])[0]).toMatchObject({
76+
usageDate: '2026-03-14',
77+
})
7578
})
7679

7780
it('increments the existing bucket instead of inserting a duplicate', async () => {

‎apps/sim/lib/uploads/contexts/execution/execution-file-manager.test.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
uploadsConfigMock,
55
uploadsConfigMockFns,
66
} from '@sim/testing/mocks/uploads-config.mock'
7+
import { toRecord } from '@sim/utils/object'
78
import { beforeEach, describe, expect, it, vi } from 'vitest'
89

910
const { mockUploadToS3, mockGetPresignedUrlWithConfig, mockDeleteFromS3 } = vi.hoisted(() => ({
@@ -72,7 +73,7 @@ describe('uploadExecutionFile key allocation', () => {
7273
const scope = { ...context, workspaceId: '11111111-1111-4111-8111-111111111111' }
7374
let persisted: Record<string, unknown> | undefined
7475
dbChainMockFns.returning.mockImplementation(async () => {
75-
persisted = dbChainMockFns.values.mock.calls.at(-1)?.[0]
76+
persisted = toRecord(dbChainMockFns.values.mock.calls.at(-1)?.[0])
7677
return [{ ...persisted }]
7778
})
7879
const uploaded = await uploadExecutionFile(
@@ -100,7 +101,7 @@ describe('uploadExecutionFile key allocation', () => {
100101
it('commits tracked provenance with the canonical file before returning its URL', async () => {
101102
const contentUpdatedAt = new Date('2026-01-01T00:00:00Z')
102103
dbChainMockFns.returning.mockImplementation(async () => {
103-
const values = dbChainMockFns.values.mock.calls.at(-1)?.[0]
104+
const values = toRecord(dbChainMockFns.values.mock.calls.at(-1)?.[0])
104105
return [{ ...values, id: values?.id ?? values?.fileId, contentUpdatedAt }]
105106
})
106107
const file = await uploadExecutionFile(
@@ -178,7 +179,7 @@ describe('uploadExecutionFile key allocation', () => {
178179
it('cleans both committed metadata and bytes when its download URL cannot be issued', async () => {
179180
const contentUpdatedAt = new Date('2026-01-01T00:00:00Z')
180181
dbChainMockFns.returning.mockImplementation(async () => {
181-
const values = dbChainMockFns.values.mock.calls.at(-1)?.[0]
182+
const values = toRecord(dbChainMockFns.values.mock.calls.at(-1)?.[0])
182183
return [{ ...values, id: values?.id ?? values?.fileId, contentUpdatedAt }]
183184
})
184185
mockGetPresignedUrlWithConfig.mockRejectedValueOnce(new Error('Signing failed'))

‎apps/sim/lib/uploads/upload-session/service.test.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
workspaceUploadsMock,
1313
workspaceUploadsMockFns,
1414
} from '@sim/testing/mocks/workspace-uploads.mock'
15+
import { toRecord } from '@sim/utils/object'
1516
import { eq, inArray, isNull } from 'drizzle-orm'
1617
import { beforeEach, describe, expect, it, vi } from 'vitest'
1718
import { createCopilotChatPrincipal } from '@/lib/mothership/auth/application-delegation'
@@ -147,11 +148,11 @@ describe('upload sessions', () => {
147148
expect(mockCreatePutTransfer).toHaveBeenCalledWith(
148149
expect.objectContaining({ key: FINAL_KEY, uploadId: 'upload-1' })
149150
)
150-
const inserted = dbChainMockFns.values.mock.calls[0][0]
151+
const inserted = toRecord(dbChainMockFns.values.mock.calls[0][0])
151152
expect(inserted.tokenHash).toBe(sha256Hex(created.uploadToken))
152153
expect(inserted.tokenHash).not.toBe(created.uploadToken)
153154
expect(inserted.finalKey).toBe(FINAL_KEY)
154-
expect(inserted.metadata.authBinding).toEqual({
155+
expect(toRecord(inserted.metadata).authBinding).toEqual({
155156
version: 1,
156157
workspaceId: WORKSPACE_ID,
157158
principal: createSessionPrincipal(),
@@ -300,7 +301,7 @@ describe('upload sessions', () => {
300301
},
301302
},
302303
})
303-
const metadata = dbChainMockFns.values.mock.calls[0][0].metadata
304+
const metadata = toRecord(toRecord(dbChainMockFns.values.mock.calls[0][0]).metadata)
304305
expect(metadata.folderId).toBe('folder')
305306
expect(Object.hasOwn(metadata, WORKSPACE_FILE_UPLOAD_PROVENANCE_KEY)).toBe(false)
306307
})
@@ -352,7 +353,7 @@ describe('upload sessions', () => {
352353
...extra,
353354
} as Parameters<typeof createUploadSession>[0])
354355

355-
const { finalKey } = dbChainMockFns.values.mock.calls[0][0]
356+
const { finalKey } = dbChainMockFns.values.mock.calls[0][0] as UploadSessionRecord
356357
const lastComponent = finalKey.slice(finalKey.lastIndexOf('/') + 1)
357358
expect(
358359
Buffer.byteLength(`${lastComponent}${LOCAL_UPLOAD_METADATA_SUFFIX}`, 'utf-8')

0 commit comments

Comments
 (0)