Skip to content

test(translate): add hermetic unit tests for lang resolution, preflight, and oneshot request/decode#229

Open
Bonny07 wants to merge 1 commit into
OwO-Network:mainfrom
Bonny07:test/translate-unit-tests
Open

test(translate): add hermetic unit tests for lang resolution, preflight, and oneshot request/decode#229
Bonny07 wants to merge 1 commit into
OwO-Network:mainfrom
Bonny07:test/translate-unit-tests

Conversation

@Bonny07

@Bonny07 Bonny07 commented Jul 8, 2026

Copy link
Copy Markdown
Collaborator

What

The translate package had no tests — this adds the first suite. Everything is hermetic: callOneshot is exercised against httptest, so go 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-hanszh-Hans), the EN/PT target-vs-source asymmetry (ENen-US as a target but en as a source), auto handling on each side, and the "unsupported code" errors.
  • TranslateByDeepLX preflight — 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: None vs Bearer <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-wide instanceID is valid.
  • oneshotRequest JSON — field order + source_lang omitempty + os_version tracking the pinned Chrome major, locking the byte-identical body contract.

Notes

  • Pure test addition — no production code changed.
  • Tests intentionally lock in current shipped behavior (e.g. empty text → 404). Where that behavior looks surprising I've left it as-is here and would rather open a separate issue to discuss than fold a behavior change into a test PR.
  • Pairs with ci: drop dead golint step, align Go to go.mod 1.25, bump checkout to v4 #228 (CI cleanup): with tests in place, adding go vet / gofmt gates to CI becomes meaningful.

Ran locally: go test ./translate/ green (36 subtests), plus gofmt, go vet, and go build ./... clean.

…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant