-
-
Notifications
You must be signed in to change notification settings - Fork 759
refactor(transformer): rename short variable names to descriptive identifiers #554
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
Closed
Closed
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| import { describe, expect, it, vi } from "vitest"; | ||
| import { | ||
| transformStreamingPayload, | ||
| deduplicateThinkingText, | ||
| cacheThinkingSignaturesFromResponse, | ||
| createThoughtBuffer, | ||
| } from "./transformer"; | ||
| import { createSignatureStore } from "../../stores/signature-store"; | ||
|
|
||
| // ─── Helpers ────────────────────────────────────────────────────────────────── | ||
|
|
||
| function geminiResponse(parts: unknown[]) { | ||
| return { candidates: [{ content: { role: "model", parts } }] }; | ||
| } | ||
|
|
||
| function thinkingPart(text: string) { | ||
| return { thought: true, text }; | ||
| } | ||
|
|
||
| function textPart(text: string) { | ||
| return { text }; | ||
| } | ||
|
|
||
| // ─── transformStreamingPayload ──────────────────────────────────────────────── | ||
|
|
||
| describe("transformStreamingPayload", () => { | ||
| it("passes non-data lines through unchanged", () => { | ||
| const line = "event: message"; | ||
| expect(transformStreamingPayload(line)).toBe(line); | ||
| }); | ||
|
|
||
| it("passes empty data line unchanged", () => { | ||
| expect(transformStreamingPayload("data: ")).toBe("data: "); | ||
| }); | ||
|
|
||
| it("passes invalid JSON data line unchanged", () => { | ||
| expect(transformStreamingPayload("data: {not json}")).toBe("data: {not json}"); | ||
| }); | ||
|
|
||
| it("passes data without response field unchanged", () => { | ||
| const line = `data: ${JSON.stringify({ candidates: [] })}`; | ||
| expect(transformStreamingPayload(line)).toBe(line); | ||
| }); | ||
|
|
||
| it("applies transformThinkingParts to response field", () => { | ||
| const inner = { type: "thinking", text: "reasoning" }; | ||
| const payload = { response: inner }; | ||
| const transform = vi.fn().mockReturnValue({ type: "redacted_thinking" }); | ||
| const result = transformStreamingPayload(`data: ${JSON.stringify(payload)}`, transform); | ||
| expect(transform).toHaveBeenCalledWith(inner); | ||
| expect(result).toContain("redacted_thinking"); | ||
| }); | ||
| }); | ||
|
|
||
| // ─── deduplicateThinkingText — Gemini candidates ────────────────────────────── | ||
|
|
||
| describe("deduplicateThinkingText", () => { | ||
| it("returns null input unchanged", () => { | ||
| expect(deduplicateThinkingText(null, createThoughtBuffer())).toBeNull(); | ||
| }); | ||
|
|
||
| it("passes non-thinking parts through", () => { | ||
| const buf = createThoughtBuffer(); | ||
| const resp = geminiResponse([textPart("hello")]); | ||
| const result = deduplicateThinkingText(resp, buf) as typeof resp; | ||
| expect(result.candidates[0].content.parts).toEqual([textPart("hello")]); | ||
| }); | ||
|
|
||
| it("emits full thinking text on first call", () => { | ||
| const buf = createThoughtBuffer(); | ||
| const result = deduplicateThinkingText(geminiResponse([thinkingPart("hello")]), buf) as any; | ||
| expect(result.candidates[0].content.parts[0].text).toBe("hello"); | ||
| }); | ||
|
|
||
| it("emits only the new delta on subsequent call with extended text", () => { | ||
| const buf = createThoughtBuffer(); | ||
| deduplicateThinkingText(geminiResponse([thinkingPart("alpha")]), buf); | ||
| const result = deduplicateThinkingText(geminiResponse([thinkingPart("alphabeta")]), buf) as any; | ||
| expect(result.candidates[0].content.parts[0].text).toBe("beta"); | ||
| }); | ||
|
|
||
| it("filters out duplicate thinking when hash set is provided", () => { | ||
| const buf = createThoughtBuffer(); | ||
| const seen = new Set<string>(); | ||
| const resp = geminiResponse([thinkingPart("same")]); | ||
| deduplicateThinkingText(resp, buf, seen); | ||
| const result2 = deduplicateThinkingText(resp, buf, seen) as any; | ||
| const parts = result2.candidates[0].content.parts; | ||
| expect(parts.some((p: any) => p.thought === true)).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| // ─── cacheThinkingSignaturesFromResponse ────────────────────────────────────── | ||
|
|
||
| describe("cacheThinkingSignaturesFromResponse", () => { | ||
| it("accumulates thinking text in the thought buffer", () => { | ||
| const store = createSignatureStore(); | ||
| const buf = createThoughtBuffer(); | ||
| cacheThinkingSignaturesFromResponse(geminiResponse([thinkingPart("my thoughts")]), "k", store, buf); | ||
| expect(buf.get(0)).toBe("my thoughts"); | ||
| }); | ||
|
|
||
| it("fires onCacheSignature with session key, text, and signature", () => { | ||
| const store = createSignatureStore(); | ||
| const buf = createThoughtBuffer(); | ||
| const onSig = vi.fn(); | ||
| cacheThinkingSignaturesFromResponse( | ||
| geminiResponse([thinkingPart("reasoning"), { thoughtSignature: "sig-1" }]), | ||
| "sess", | ||
| store, | ||
| buf, | ||
| onSig, | ||
| ); | ||
| expect(onSig).toHaveBeenCalledWith("sess", "reasoning", "sig-1"); | ||
| }); | ||
|
|
||
| it("stores result in signatureStore keyed by session key", () => { | ||
| const store = createSignatureStore(); | ||
| const buf = createThoughtBuffer(); | ||
| cacheThinkingSignaturesFromResponse( | ||
| geminiResponse([thinkingPart("t"), { thoughtSignature: "sig-2" }]), | ||
| "session-a", | ||
| store, | ||
| buf, | ||
| ); | ||
| expect(store.get("session-a")).toEqual({ text: "t", signature: "sig-2" }); | ||
| }); | ||
|
|
||
| it("skips firing onCacheSignature when no thinking text was accumulated", () => { | ||
| const store = createSignatureStore(); | ||
| const buf = createThoughtBuffer(); | ||
| const onSig = vi.fn(); | ||
| cacheThinkingSignaturesFromResponse(geminiResponse([{ thoughtSignature: "sig" }]), "k", store, buf, onSig); | ||
| expect(onSig).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
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.
Fix the strict-nullability error in this assertion.
tsc --noEmitis failing here becauseresult.candidates[0]can still beundefinedunder strict indexed access. Add a tighter helper type or a non-null assertion before dereferencing.🔧 Proposed fix
📝 Committable suggestion
🧰 Tools
🪛 GitHub Actions: CI
[error] 66-66: TypeScript typecheck (tsc --noEmit) failed: TS2532 Object is possibly 'undefined'.
🪛 GitHub Check: Test on Node.js
[failure] 66-66:
Object is possibly 'undefined'.
🤖 Prompt for AI Agents