-
Notifications
You must be signed in to change notification settings - Fork 2
Sync with InsForge-sdk-js v1.5.0 #7
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
Open
Fermionic-Lyu
wants to merge
1
commit into
main
Choose a base branch
from
sdk-sync/v1.5.0
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+139
−22
Open
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
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,4 +1,5 @@ | ||||||
| import asyncio | ||||||
| import re | ||||||
| from pathlib import Path | ||||||
| import tomllib | ||||||
|
|
||||||
|
|
@@ -332,16 +333,16 @@ async def scenario() -> tuple[list[dict[str, object]], object, object, object]: | |||||
|
|
||||||
| async def fake_request(method: str, url: httpx.URL, **kwargs: object) -> httpx.Response: | ||||||
| calls.append({"method": method, "url": str(url), "kwargs": kwargs}) | ||||||
| if url.path.endswith("/objects"): | ||||||
| if method == "PUT": | ||||||
| return httpx.Response( | ||||||
| 201, | ||||||
| json={ | ||||||
| "bucket": "avatars", | ||||||
| "key": "auto.jpg", | ||||||
| "key": url.path.rsplit("/objects/", 1)[-1], | ||||||
| "size": 10, | ||||||
| "mimeType": "image/jpeg", | ||||||
| "uploadedAt": "2024-01-01T00:00:00Z", | ||||||
| "url": "/api/storage/buckets/avatars/objects/auto.jpg", | ||||||
| "url": str(url.path), | ||||||
| }, | ||||||
| ) | ||||||
| if url.path.endswith("/confirm-upload"): | ||||||
|
|
@@ -383,8 +384,11 @@ async def fake_request(method: str, url: httpx.URL, **kwargs: object) -> httpx.R | |||||
|
|
||||||
| calls, auto, confirm, upload_strategy, download_strategy = asyncio.run(scenario()) | ||||||
|
|
||||||
| assert calls[0]["method"] == "POST" and calls[0]["url"].endswith("/objects") | ||||||
| assert auto.key == "auto.jpg" | ||||||
| # upload_object_auto mints a unique key client-side and uploads via the | ||||||
| # standard PUT route — the backend no longer generates keys. | ||||||
| assert calls[0]["method"] == "PUT" | ||||||
| assert re.search(r"/objects/auto-\d+-[a-z0-9]{6}\.jpg$", calls[0]["url"]) | ||||||
| assert re.fullmatch(r"auto-\d+-[a-z0-9]{6}\.jpg", auto.key) | ||||||
|
|
||||||
| assert calls[1]["method"] == "POST" and calls[1]["url"].endswith("/confirm-upload") | ||||||
| assert calls[1]["kwargs"]["json"] == {"size": 10, "etag": "etag123"} | ||||||
|
|
@@ -403,6 +407,96 @@ async def fake_request(method: str, url: httpx.URL, **kwargs: object) -> httpx.R | |||||
| assert download_strategy.method == "direct" | ||||||
|
|
||||||
|
|
||||||
| def test_upload_object_auto_mints_key_client_side_and_uses_put() -> None: | ||||||
| async def scenario() -> tuple[object, dict[str, object]]: | ||||||
| captured: dict[str, object] = {} | ||||||
|
|
||||||
| async def fake_request(method: str, url: httpx.URL, **kwargs: object) -> httpx.Response: | ||||||
| captured["method"] = method | ||||||
| captured["url"] = str(url) | ||||||
| captured["kwargs"] = kwargs | ||||||
| key = url.path.rsplit("/objects/", 1)[-1] | ||||||
| return httpx.Response( | ||||||
| 201, | ||||||
| json={ | ||||||
| "bucket": "docs", | ||||||
| "key": key, | ||||||
| "size": 3, | ||||||
| "mimeType": "application/pdf", | ||||||
| "uploadedAt": "2026-01-01T00:00:00Z", | ||||||
| "url": str(url.path), | ||||||
| }, | ||||||
| ) | ||||||
|
|
||||||
| async with InsforgeClient( | ||||||
| base_url="https://example.com", | ||||||
| api_key="ins_test", | ||||||
| ) as client: | ||||||
| client.http_client.request = fake_request # type: ignore[method-assign] | ||||||
| result = await client.storage.upload_object_auto( | ||||||
| "docs", | ||||||
| data=b"pdf", | ||||||
| filename="report.pdf", | ||||||
| content_type="application/pdf", | ||||||
| ) | ||||||
| return result, captured | ||||||
|
|
||||||
| result, captured = asyncio.run(scenario()) | ||||||
|
|
||||||
| # Client-generated key: sanitized base + timestamp + random, preserving ext. | ||||||
| assert captured["method"] == "PUT" | ||||||
| assert re.fullmatch(r"report-\d+-[a-z0-9]{6}\.pdf", result.key) | ||||||
| assert captured["url"].endswith(f"/api/storage/buckets/docs/objects/{result.key}") | ||||||
| assert captured["kwargs"]["files"]["file"] == (result.key, b"pdf", "application/pdf") | ||||||
|
|
||||||
|
|
||||||
| def test_upload_object_auto_generates_distinct_keys_for_same_filename() -> None: | ||||||
| async def scenario() -> list[str]: | ||||||
| keys: list[str] = [] | ||||||
|
|
||||||
| async def fake_request(method: str, url: httpx.URL, **kwargs: object) -> httpx.Response: | ||||||
| key = url.path.rsplit("/objects/", 1)[-1] | ||||||
| keys.append(key) | ||||||
| return httpx.Response( | ||||||
| 201, | ||||||
| json={ | ||||||
| "bucket": "docs", | ||||||
| "key": key, | ||||||
| "size": 3, | ||||||
| "mimeType": "application/octet-stream", | ||||||
| "uploadedAt": "2026-01-01T00:00:00Z", | ||||||
| "url": str(url.path), | ||||||
| }, | ||||||
| ) | ||||||
|
|
||||||
| async with InsforgeClient(base_url="https://example.com", api_key="ins_test") as client: | ||||||
| client.http_client.request = fake_request # type: ignore[method-assign] | ||||||
| await client.storage.upload_object_auto("docs", data=b"abc", filename="photo.png") | ||||||
| await client.storage.upload_object_auto("docs", data=b"abc", filename="photo.png") | ||||||
| return keys | ||||||
|
|
||||||
| keys = asyncio.run(scenario()) | ||||||
|
|
||||||
| assert len(keys) == 2 | ||||||
| assert keys[0] != keys[1] | ||||||
|
|
||||||
|
|
||||||
| def test_generate_object_key_sanitizes_base_and_falls_back_to_file() -> None: | ||||||
| from insforge.storage.client import _generate_object_key | ||||||
|
|
||||||
| # Non-alphanumeric characters in the base are replaced, extension kept. | ||||||
| assert re.fullmatch(r"my-r-sum--v2-\d+-[a-z0-9]{6}\.pdf", _generate_object_key("my résumé v2.pdf")) | ||||||
| # Base longer than 32 chars is truncated. | ||||||
| key = _generate_object_key("a" * 50 + ".txt") | ||||||
| assert re.fullmatch(r"a{32}-\d+-[a-z0-9]{6}\.txt", key) | ||||||
| # Disallowed characters are each replaced with a dash. | ||||||
| assert re.fullmatch(r"----\d+-[a-z0-9]{6}", _generate_object_key("日本語")) | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P1: Prompt for AI agents
Suggested change
|
||||||
| # An empty base falls back to "file". | ||||||
| assert re.fullmatch(r"file-\d+-[a-z0-9]{6}", _generate_object_key("")) | ||||||
| # A leading dot is not treated as an extension separator. | ||||||
| assert re.fullmatch(r"-gitignore-\d+-[a-z0-9]{6}", _generate_object_key(".gitignore")) | ||||||
|
|
||||||
|
|
||||||
| def test_storage_encoding_for_new_admin_paths() -> None: | ||||||
| async def scenario() -> list[dict[str, object]]: | ||||||
| calls: list[dict[str, object]] = [] | ||||||
|
|
||||||
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.
P1: This assertion will raise a
KeyErrorbecauseupload_objectsends data ascontent=data, not as multipartfiles=.... Theupload_object_automethod now delegates toupload_object, which makes aPUTrequest withcontent=data(raw bytes) and aContent-Typeheader — not with thefiles=parameter that multipart uploads use. Thecaptured["kwargs"]dict will contain"content"and"headers"keys, not"files".Prompt for AI agents