@workflow/web: Decompose workflow-api-client.ts into focused modules and add test infrastructure#1070
Open
alizenhom wants to merge 7 commits intovercel:mainfrom
Open
@workflow/web: Decompose workflow-api-client.ts into focused modules and add test infrastructure#1070alizenhom wants to merge 7 commits intovercel:mainfrom
@workflow/web: Decompose workflow-api-client.ts into focused modules and add test infrastructure#1070alizenhom wants to merge 7 commits intovercel:mainfrom
Conversation
🦋 Changeset detectedLatest commit: 7bbe71c The changes in this PR will be included in the next version bump. This PR includes changesets to release 15 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Contributor
|
@alizenhom is attempting to deploy a commit to the Vercel Labs Team on Vercel. A member of the Team first needs to authorize it. |
e904ffd to
50f626a
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
workflow-api-client.ts(~1,330 lines) into 8 focused, single-responsibility modules while preserving a barrel re-export for backward compatibilityvitest+jsdom+@testing-library/reacttest infrastructure for packages/web and adds comprehensive unit tests across all extracted modulesMotivation
workflow-api-client.tshad grown to 1,329 lines with extensive duplication across 5 distinct patterns — paginated list hooks, exhaustive fetch helpers, merge-by-ID functions, poll functions, and server action wrappers — each repeated 2-3 times with only the fetch call or ID key varying. This made it difficult to test, navigate, and reason about individual concerns, and the package had no unit tests or test runner configured.What Changed?
The monolith was split along natural boundaries into standalone modules:
workflow-errors.ts—>WorkflowWebAPIErrorclass,getErrorMessage,unwrapServerActionResult, andunwrapOrThrowhelpers for consistent error handling across the packageworkflow-primitives.ts—> Pure utility functions (mergeById,fetchAllPaginated,pollResource) that provide the generic pagination and polling building blocks used by the hooksworkflow-actions.ts—> Thin wrappers around server actions (cancelRun,recreateRun,reenqueueRun,wakeUpRun,resumeHook) with uniform error unwrappingworkflow-streams.ts—>readStreamandlistStreamsfor fetching workflow stream data via the Fetch APIhooks/:use-paginated-list.ts—> GenericusePaginatedListhook with page caching and navigation, plus concreteuseWorkflowRunsanduseWorkflowHookshooks built on top of ituse-resource-data.ts—>useWorkflowResourceDatafor fetching individual run/step/hook/sleep detail views with auto-refreshuse-trace-viewer.ts—>useWorkflowTraceViewerDatafor the trace viewer, combining run, steps, hooks, and events with live polling and exhaustive paginationuse-workflow-streams.ts—>useWorkflowStreamsfor polling the list of stream IDs on a runworkflow-api-client.tsis now a ~39-line barrel that re-exports everything, so existing consumers are unaffected.Test infrastructure
vitest,jsdom, and@testing-library/reactas dev dependenciesvitest.config.tswithjsdomenvironment and ~ path aliaspnpm test)Test coverage
Added 8 test files organized to mirror the module structure:
Utilities & core logic
workflow-errors.test.ts—> error class construction, 403 message handling, unwrapOrThrow success/failure/rejection pathsworkflow-primitives.test.ts—> mergeById deduplication and custom ID keys, fetchAllPaginated multi-page traversal and error bail-out, pollResource with both always and onHasMore cursor strategiesworkflow-actions.test.ts—> success and failure cases for all 5 action wrappersworkflow-streams.test.ts—> readStream with startIndex/abort signal/non-ok responses/null body, listStreams success and structured error parsingReact hooks (using
@testing-library/react+jsdom)use-paginated-list.test.ts—> initial fetch, error propagation, parameter forwarding, cursor-based nextPage, previousPage cache hit, and reload (covers both useWorkflowRuns and useWorkflowHooks)use-resource-data.test.ts—> run/step/hook/sleep resource types, enabled: false short-circuit, error statesuse-trace-viewer.test.ts—> combined initial load of run + steps + hooks + events, paginated step population, error handlinguse-workflow-streams.test.ts—> initial fetch and error handlingNotes
Apologies for the size of this PR, the extraction touched a lot of files but the actual logic is unchanged. Happy to break it up differently or address any feedback.
As a new contributor still getting familiar with the codebase, there may be rough edges I've missed. I'd appreciate any guidance on the following directions I'm interested in exploring next:
Would love to know if either of these aligns with the project's priorities!