test(translate): add hermetic unit tests for lang resolution, preflight, and oneshot request/decode#229
Open
Bonny07 wants to merge 1 commit into
Open
Conversation
…ht, and oneshot request/decode The translate package had no tests. These are fully hermetic: callOneshot is exercised against httptest, so go test ./... stays offline and fast. Covers lang resolution (incl. the EN/PT target-vs-source asymmetry), TranslateByDeepLX preflight validation (404/400/413, pre-network only), callOneshot request shaping and gzip/deflate/br decoding, newInstanceID UUIDv4 shape, and the oneshotRequest JSON contract.
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.
What
The
translatepackage had no tests — this adds the first suite. Everything is hermetic:callOneshotis exercised againsthttptest, sogo test ./...stays fast and offline (no live DeepL calls), and the existing CI test step covers it with no workflow change.Coverage
resolveTargetLang/resolveSourceLang— case-insensitive normalization (zh-hans→zh-Hans), the EN/PT target-vs-source asymmetry (EN→en-USas a target butenas a source),autohandling on each side, and the "unsupported code" errors.TranslateByDeepLXpreflight — empty text → 404, invalid language → 400, and over-maxFreeTextLength→ 413, including a multibyte case that proves the cap counts runes, not bytes. Only the pre-network validation paths are exercised, so no upstream request is made.callOneshot— request shaping (Authorization: NonevsBearer <token>,Content-Type/Origin/Sec-Fetch-*, body pass-through) and the three hand-rolled decode paths (gzip / deflate / br) plus identity.newInstanceID— RFC 4122 v4 shape, and that the process-wideinstanceIDis valid.oneshotRequestJSON — field order +source_langomitempty+os_versiontracking the pinned Chrome major, locking the byte-identical body contract.Notes
go vet/gofmtgates to CI becomes meaningful.Ran locally:
go test ./translate/green (36 subtests), plusgofmt,go vet, andgo build ./...clean.