chore: import upstream snapshot with attribution
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
import { headers } from 'next/headers'
|
||||
import { getInternalApiBaseUrl } from '@/lib/core/utils/urls'
|
||||
|
||||
/**
|
||||
* Server-side GET against an internal `/api` route, forwarding the incoming
|
||||
* request's cookie so the route authenticates as the current user.
|
||||
*
|
||||
* List prefetches go through the route (rather than the data layer) when the
|
||||
* payload carries `Date` fields: `NextResponse.json` serializes them to the
|
||||
* string wire shape the client caches via `requestJson`, so the
|
||||
* server-hydrated entry byte-matches the client-fetched one through
|
||||
* dehydration. Calling the data layer directly would cache raw `Date` objects
|
||||
* and drift from that wire shape. Mirrors the settings/subscription prefetch.
|
||||
*/
|
||||
export async function prefetchInternalJson<T>(path: string): Promise<T> {
|
||||
const cookie = (await headers()).get('cookie')
|
||||
// boundary-raw-fetch: server-side RSC prefetch forwarding the session cookie to an internal API route; requestJson is client-only and cannot run here
|
||||
const response = await fetch(`${getInternalApiBaseUrl()}${path}`, {
|
||||
headers: cookie ? { cookie } : {},
|
||||
})
|
||||
if (!response.ok) {
|
||||
throw new Error(`Prefetch failed for ${path}: ${response.status}`)
|
||||
}
|
||||
return response.json() as Promise<T>
|
||||
}
|
||||
@@ -0,0 +1,169 @@
|
||||
/**
|
||||
* @vitest-environment node
|
||||
*/
|
||||
import { QueryClient } from '@tanstack/react-query'
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
|
||||
const { mockPrefetchInternalJson } = vi.hoisted(() => ({
|
||||
mockPrefetchInternalJson: vi.fn(),
|
||||
}))
|
||||
|
||||
vi.mock('@/app/workspace/[workspaceId]/lib/prefetch-internal-fetch', () => ({
|
||||
prefetchInternalJson: mockPrefetchInternalJson,
|
||||
}))
|
||||
|
||||
vi.mock('@sim/emcn', () => ({
|
||||
toast: { success: vi.fn(), error: vi.fn() },
|
||||
}))
|
||||
|
||||
import { prefetchFilesBrowser } from '@/app/workspace/[workspaceId]/files/prefetch'
|
||||
import { prefetchHomeLists } from '@/app/workspace/[workspaceId]/home/prefetch'
|
||||
import { prefetchKnowledgeBases } from '@/app/workspace/[workspaceId]/knowledge/prefetch'
|
||||
import { prefetchTables } from '@/app/workspace/[workspaceId]/tables/prefetch'
|
||||
import { knowledgeKeys } from '@/hooks/queries/kb/knowledge'
|
||||
import { folderKeys } from '@/hooks/queries/utils/folder-keys'
|
||||
import { tableKeys } from '@/hooks/queries/utils/table-keys'
|
||||
import { workspaceFileFolderKeys } from '@/hooks/queries/workspace-file-folders'
|
||||
import { workspaceFilesKeys } from '@/hooks/queries/workspace-files'
|
||||
|
||||
const WORKSPACE_ID = 'ws-123'
|
||||
|
||||
function makeClient() {
|
||||
return new QueryClient({ defaultOptions: { queries: { retry: false } } })
|
||||
}
|
||||
|
||||
describe('workspace list prefetches', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
describe('prefetchKnowledgeBases', () => {
|
||||
it('primes the exact key useKnowledgeBasesQuery reads and unwraps data', async () => {
|
||||
const bases = [{ id: 'kb-1' }]
|
||||
mockPrefetchInternalJson.mockResolvedValue({ data: bases })
|
||||
const client = makeClient()
|
||||
|
||||
await prefetchKnowledgeBases(client, WORKSPACE_ID)
|
||||
|
||||
expect(mockPrefetchInternalJson).toHaveBeenCalledWith(
|
||||
`/api/knowledge?workspaceId=${WORKSPACE_ID}&scope=active`
|
||||
)
|
||||
expect(client.getQueryData(knowledgeKeys.list(WORKSPACE_ID, 'active'))).toEqual(bases)
|
||||
})
|
||||
})
|
||||
|
||||
describe('prefetchTables', () => {
|
||||
it('primes the exact key useTablesList reads and unwraps data.tables', async () => {
|
||||
const tables = [{ id: 't-1' }]
|
||||
mockPrefetchInternalJson.mockResolvedValue({ data: { tables } })
|
||||
const client = makeClient()
|
||||
|
||||
await prefetchTables(client, WORKSPACE_ID)
|
||||
|
||||
expect(mockPrefetchInternalJson).toHaveBeenCalledWith(
|
||||
`/api/table?workspaceId=${WORKSPACE_ID}&scope=active`
|
||||
)
|
||||
expect(client.getQueryData(tableKeys.list(WORKSPACE_ID, 'active'))).toEqual(tables)
|
||||
})
|
||||
})
|
||||
|
||||
describe('prefetchFilesBrowser', () => {
|
||||
it('primes both file + folder keys the client hooks read', async () => {
|
||||
const files = [{ id: 'f-1' }]
|
||||
const folders = [{ id: 'folder-1' }]
|
||||
mockPrefetchInternalJson.mockImplementation(async (path: string) =>
|
||||
path.includes('/folders') ? { folders } : { success: true, files }
|
||||
)
|
||||
const client = makeClient()
|
||||
|
||||
await prefetchFilesBrowser(client, WORKSPACE_ID)
|
||||
|
||||
expect(mockPrefetchInternalJson).toHaveBeenCalledWith(
|
||||
`/api/workspaces/${WORKSPACE_ID}/files?scope=active`
|
||||
)
|
||||
expect(mockPrefetchInternalJson).toHaveBeenCalledWith(
|
||||
`/api/workspaces/${WORKSPACE_ID}/files/folders?scope=active`
|
||||
)
|
||||
expect(client.getQueryData(workspaceFilesKeys.list(WORKSPACE_ID, 'active'))).toEqual(files)
|
||||
expect(client.getQueryData(workspaceFileFolderKeys.list(WORKSPACE_ID, 'active'))).toEqual(
|
||||
folders
|
||||
)
|
||||
})
|
||||
|
||||
it('caches an empty file list when the route reports failure', async () => {
|
||||
mockPrefetchInternalJson.mockImplementation(async (path: string) =>
|
||||
path.includes('/folders') ? { folders: [] } : { success: false, files: [] }
|
||||
)
|
||||
const client = makeClient()
|
||||
|
||||
await prefetchFilesBrowser(client, WORKSPACE_ID)
|
||||
|
||||
expect(client.getQueryData(workspaceFilesKeys.list(WORKSPACE_ID, 'active'))).toEqual([])
|
||||
})
|
||||
})
|
||||
|
||||
describe('prefetchHomeLists', () => {
|
||||
it('primes folder + file keys, mapping folder rows to the client shape', async () => {
|
||||
const folderRow = {
|
||||
id: 'folder-1',
|
||||
name: 'Docs',
|
||||
userId: 'u-1',
|
||||
workspaceId: WORKSPACE_ID,
|
||||
parentId: null,
|
||||
color: null,
|
||||
isExpanded: true,
|
||||
locked: false,
|
||||
sortOrder: 0,
|
||||
createdAt: '2026-01-01T00:00:00.000Z',
|
||||
updatedAt: '2026-01-02T00:00:00.000Z',
|
||||
archivedAt: null,
|
||||
}
|
||||
const files = [{ id: 'f-1' }]
|
||||
mockPrefetchInternalJson.mockImplementation(async (path: string) =>
|
||||
path.startsWith('/api/folders') ? { folders: [folderRow] } : { success: true, files }
|
||||
)
|
||||
const client = makeClient()
|
||||
|
||||
await prefetchHomeLists(client, WORKSPACE_ID)
|
||||
|
||||
expect(mockPrefetchInternalJson).toHaveBeenCalledWith(
|
||||
`/api/folders?workspaceId=${WORKSPACE_ID}&scope=active`
|
||||
)
|
||||
const cachedFolders = client.getQueryData(folderKeys.list(WORKSPACE_ID, 'active')) as Array<{
|
||||
id: string
|
||||
color: string
|
||||
createdAt: Date
|
||||
}>
|
||||
expect(cachedFolders).toHaveLength(1)
|
||||
expect(cachedFolders[0].color).toBe('#6B7280')
|
||||
expect(cachedFolders[0].createdAt).toBeInstanceOf(Date)
|
||||
expect(client.getQueryData(workspaceFilesKeys.list(WORKSPACE_ID, 'active'))).toEqual(files)
|
||||
})
|
||||
})
|
||||
|
||||
describe('graceful failure', () => {
|
||||
it.each([
|
||||
[
|
||||
'prefetchKnowledgeBases',
|
||||
prefetchKnowledgeBases,
|
||||
knowledgeKeys.list(WORKSPACE_ID, 'active'),
|
||||
],
|
||||
['prefetchTables', prefetchTables, tableKeys.list(WORKSPACE_ID, 'active')],
|
||||
['prefetchHomeLists', prefetchHomeLists, folderKeys.list(WORKSPACE_ID, 'active')],
|
||||
[
|
||||
'prefetchFilesBrowser',
|
||||
prefetchFilesBrowser,
|
||||
workspaceFilesKeys.list(WORKSPACE_ID, 'active'),
|
||||
],
|
||||
] as const)(
|
||||
'%s does not throw when the fetcher rejects (page still renders, client refetches)',
|
||||
async (_name, prefetch, queryKey) => {
|
||||
mockPrefetchInternalJson.mockRejectedValue(new Error('500'))
|
||||
const client = makeClient()
|
||||
|
||||
await expect(prefetch(client, WORKSPACE_ID)).resolves.toBeUndefined()
|
||||
expect(client.getQueryData(queryKey)).toBeUndefined()
|
||||
}
|
||||
)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user