-
-
Notifications
You must be signed in to change notification settings - Fork 169
fix(uploads): detect upload success via response.ok so the editor stops failing every image #1339
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
+61
−7
Merged
Changes from all commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import { describe, it, expect, vi, afterEach } from "vitest"; | ||
| import { uploadFile } from "./s3helpers"; | ||
|
|
||
| // Regression guard: spreading a Response (`{ ...response }`) dropped its | ||
| // prototype getters, so `result.ok` came back `undefined` and the editor | ||
| // reported failure on every upload — even on a 200. | ||
| describe("uploadFile", () => { | ||
| afterEach(() => { | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| function mockPutResponse(status: number, url: string) { | ||
| const response = new Response(null, { status }); | ||
| // Response.url is read-only via the constructor; pin it for fileLocation. | ||
| Object.defineProperty(response, "url", { value: url }); | ||
| vi.stubGlobal("fetch", vi.fn().mockResolvedValue(response)); | ||
| } | ||
|
|
||
| const file = new Blob(["x"], { type: "image/png" }) as unknown as File; | ||
|
|
||
| it("exposes ok=true on a successful PUT so callers can detect success", async () => { | ||
| mockPutResponse( | ||
| 200, | ||
| "https://bucket.s3.amazonaws.com/uploads/u1/abc.png?sig=x", | ||
| ); | ||
|
|
||
| const result = await uploadFile("https://signed-url", file); | ||
|
|
||
| expect(result.ok).toBe(true); | ||
| expect(result.fileLocation).toBe( | ||
| "https://bucket.s3.amazonaws.com/uploads/u1/abc.png", | ||
| ); | ||
| }); | ||
|
|
||
| it("exposes ok=false when S3 rejects the PUT", async () => { | ||
| mockPutResponse( | ||
| 403, | ||
| "https://bucket.s3.amazonaws.com/uploads/u1/abc.png?sig=x", | ||
| ); | ||
|
|
||
| const result = await uploadFile("https://signed-url", file); | ||
|
|
||
| expect(result.ok).toBe(false); | ||
| }); | ||
| }); | ||
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
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.
🧩 Analysis chain
🌐 Web query:
In Vitest v4.x, doesvi.restoreAllMocks()revert globals stubbed withvi.stubGlobal()? If not, isvi.unstubAllGlobals()the intended cleanup API?💡 Result:
In Vitest v4.x, vi.restoreAllMocks does not revert global variables stubbed with vi.stubGlobal [1][2]. vi.restoreAllMocks is specifically designed to restore the original implementations of spies created via vi.spyOn [1][2]. vi.unstubAllGlobals is the correct and intended API for cleaning up stubs created with vi.stubGlobal [1][3]. When you call vi.stubGlobal, Vitest tracks the original value (or state) of the global variable so that vi.unstubAllGlobals can restore it to its previous state [1][3]. Alternatively, you can enable the unstubGlobals configuration option in your Vitest config file to have Vitest automatically call vi.unstubAllGlobals after each test [4][5].
Citations:
🏁 Script executed:
fd "s3helpers.test.ts" -x wc -l {}Repository: codu-code/codu
Length of output: 87
🏁 Script executed:
fd "s3helpers.test.ts" -x cat -n {}Repository: codu-code/codu
Length of output: 1844
Unstub global
fetchin teardown to prevent test-suite leakage.The
afterEachblock at lines 8-10 callsvi.restoreAllMocks(), but this does not clean up globals stubbed withvi.stubGlobal("fetch", ...)at line 16. Each test will leave the stubbed fetch in place, potentially affecting subsequent tests. Addvi.unstubAllGlobals()to the teardown:Suggested patch
afterEach(() => { vi.restoreAllMocks(); + vi.unstubAllGlobals(); });🤖 Prompt for AI Agents