Repository navigation
fix(package-apps): take D1 and run-record begin off the warm fetch path #1410
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
185 changes: 185 additions & 0 deletions
185
packages/worker/src/package-runtime/package-app-serve.node.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,185 @@ | ||
| import { expect, test, vi } from 'vitest' | ||
| import { servePackageAppRequest } from './package-app-serve.ts' | ||
|
|
||
| const mockModule = vi.hoisted(() => ({ | ||
| getSavedPackageById: vi.fn(), | ||
| getSavedPackageByKodyId: vi.fn(), | ||
| getEntitySourceById: vi.fn(), | ||
| loadPublishedEntityManifest: vi.fn(), | ||
| buildPackageAppWorker: vi.fn(), | ||
| })) | ||
|
|
||
| vi.mock('#worker/package-registry/repo.ts', () => ({ | ||
| getSavedPackageById: (...args: Array<unknown>) => | ||
| mockModule.getSavedPackageById(...args), | ||
| getSavedPackageByKodyId: (...args: Array<unknown>) => | ||
| mockModule.getSavedPackageByKodyId(...args), | ||
| })) | ||
|
|
||
| vi.mock('#worker/repo/entity-sources.ts', () => ({ | ||
| getEntitySourceById: (...args: Array<unknown>) => | ||
| mockModule.getEntitySourceById(...args), | ||
| })) | ||
|
|
||
| vi.mock('#worker/repo/published-source.ts', () => ({ | ||
| loadPublishedEntityManifest: (...args: Array<unknown>) => | ||
| mockModule.loadPublishedEntityManifest(...args), | ||
| loadPublishedEntitySource: vi.fn(), | ||
| })) | ||
|
|
||
| vi.mock('#worker/package-runtime/package-app.ts', () => ({ | ||
| createPackageAppCallerContext: async (input: { | ||
| user: { userId: string; email: string; displayName: string } | ||
| }) => ({ | ||
| user: input.user, | ||
| }), | ||
| buildPackageAppWorker: (...args: Array<unknown>) => | ||
| mockModule.buildPackageAppWorker(...args), | ||
| })) | ||
|
|
||
| const serveLoadMocks = [ | ||
| ['saved package by id (D1)', mockModule.getSavedPackageById], | ||
| ['saved package by kody id (D1)', mockModule.getSavedPackageByKodyId], | ||
| ['entity source row (D1)', mockModule.getEntitySourceById], | ||
| ['published manifest snapshot (KV)', mockModule.loadPublishedEntityManifest], | ||
| ] as const | ||
|
|
||
| function countServeLoads() { | ||
| return Object.fromEntries( | ||
| serveLoadMocks.map(([label, mock]) => [label, mock.mock.calls.length]), | ||
| ) | ||
| } | ||
|
|
||
| function clearServeLoadCounters() { | ||
| for (const [, mock] of serveLoadMocks) { | ||
| mock.mockClear() | ||
| } | ||
| } | ||
|
|
||
| function createFixture() { | ||
| const sourceId = 'source-perf-app' | ||
| const savedPackage = { | ||
| id: 'pkg-perf-app', | ||
| userId: 'user-1', | ||
| name: '@kentcdodds/perf-app', | ||
| kodyId: 'perf-app', | ||
| description: 'Minimal hello-world app', | ||
| tags: [], | ||
| searchText: null, | ||
| sourceId, | ||
| hasApp: true, | ||
| hidden: true, | ||
| isPrivate: true, | ||
| createdAt: '2026-08-12T00:00:00.000Z', | ||
| updatedAt: '2026-08-12T00:00:00.000Z', | ||
| } | ||
| const source = { | ||
| id: sourceId, | ||
| user_id: 'user-1', | ||
| entity_kind: 'package' as const, | ||
| entity_id: savedPackage.id, | ||
| repo_id: 'repo-perf-app', | ||
| published_commit: 'commit-1', | ||
| indexed_commit: 'commit-1', | ||
| manifest_path: 'package.json', | ||
| source_root: '/', | ||
| last_external_check_at: null, | ||
| external_check_until: null, | ||
| created_at: '2026-08-12T00:00:00.000Z', | ||
| updated_at: '2026-08-12T00:00:00.000Z', | ||
| } | ||
| const manifestContent = JSON.stringify({ | ||
| name: '@kentcdodds/perf-app', | ||
| private: true, | ||
| exports: { '.': './src/index.ts' }, | ||
| kody: { | ||
| id: 'perf-app', | ||
| description: 'Minimal hello-world app', | ||
| app: { entry: './src/app.ts' }, | ||
| }, | ||
| }) | ||
| return { savedPackage, source, manifestContent } | ||
| } | ||
|
|
||
| function seedFixture() { | ||
| const fixture = createFixture() | ||
| mockModule.getSavedPackageById.mockResolvedValue(null) | ||
| mockModule.getSavedPackageByKodyId.mockImplementation( | ||
| async (_db: unknown, input: { userId: string; kodyId: string }) => | ||
| input.userId === fixture.savedPackage.userId && | ||
| input.kodyId === fixture.savedPackage.kodyId | ||
| ? fixture.savedPackage | ||
| : null, | ||
| ) | ||
| mockModule.getEntitySourceById.mockImplementation( | ||
| async (_db: unknown, sourceId: string) => | ||
| sourceId === fixture.source.id ? fixture.source : null, | ||
| ) | ||
| mockModule.loadPublishedEntityManifest.mockImplementation( | ||
| async (input: { sourceId: string }) => { | ||
| if (input.sourceId !== fixture.source.id) { | ||
| throw new Error(`No manifest for ${input.sourceId}`) | ||
| } | ||
| return { source: fixture.source, content: fixture.manifestContent } | ||
| }, | ||
| ) | ||
| mockModule.buildPackageAppWorker.mockResolvedValue({ | ||
| entrypointName: 'PackageAppWorker', | ||
| stub: { | ||
| getEntrypoint: () => ({ | ||
| async fetch() { | ||
| return new Response('ok') | ||
| }, | ||
| }), | ||
| }, | ||
| }) | ||
| return fixture | ||
| } | ||
|
|
||
| async function serveHelloWorld() { | ||
| return await servePackageAppRequest({ | ||
| request: new Request('https://example.com/@kentcdodds/packages/perf-app'), | ||
| env: { | ||
| APP_DB: {}, | ||
| BUNDLE_ARTIFACTS_KV: {}, | ||
| } as Env, | ||
| owner: { | ||
| userId: 'user-1', | ||
| username: 'kentcdodds', | ||
| email: 'kent@example.com', | ||
| displayName: 'Kent', | ||
| }, | ||
| packagePath: { | ||
| username: 'kentcdodds', | ||
| kodyId: 'perf-app', | ||
| restPath: '/', | ||
| mount: 'username-path', | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| test('a warm package-app serve performs zero D1/KV loads before dispatch', async () => { | ||
| seedFixture() | ||
|
|
||
| const cold = await serveHelloWorld() | ||
| expect(cold.status).toBe(200) | ||
| expect(await cold.text()).toBe('ok') | ||
| expect(mockModule.getSavedPackageByKodyId).toHaveBeenCalledTimes(1) | ||
| expect(mockModule.getEntitySourceById).toHaveBeenCalledTimes(1) | ||
| expect(mockModule.loadPublishedEntityManifest).toHaveBeenCalledTimes(1) | ||
| expect(mockModule.buildPackageAppWorker).toHaveBeenCalledTimes(1) | ||
|
|
||
| clearServeLoadCounters() | ||
| mockModule.buildPackageAppWorker.mockClear() | ||
| const warm = await serveHelloWorld() | ||
|
|
||
| expect(warm.status).toBe(200) | ||
| expect(await warm.text()).toBe('ok') | ||
| expect(countServeLoads()).toEqual({ | ||
| 'saved package by id (D1)': 0, | ||
| 'saved package by kody id (D1)': 0, | ||
| 'entity source row (D1)': 0, | ||
| 'published manifest snapshot (KV)': 0, | ||
| }) | ||
| expect(mockModule.buildPackageAppWorker).toHaveBeenCalledTimes(1) | ||
| }) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🗄️ Data Integrity & Integration | 🟡 Minor | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: kentcdodds/kody
Length of output: 50372
🏁 Script executed:
Repository: kentcdodds/kody
Length of output: 50372
🏁 Script executed:
Repository: kentcdodds/kody
Length of output: 18047
🏁 Script executed:
Repository: kentcdodds/kody
Length of output: 50372
🏁 Script executed:
Repository: kentcdodds/kody
Length of output: 50372
🏁 Script executed:
Repository: kentcdodds/kody
Length of output: 50372
🏁 Script executed:
Repository: kentcdodds/kody
Length of output: 1919
Include the lookup kind in the cache key.
idandkody_iduse different lookup queries, but the schema does not make their namespaces disjoint. Prefix the key withpackage-idorkody-idto prevent one lookup from returning another package’s cached record.🤖 Prompt for AI Agents