File size: 6,968 Bytes
251a574
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import { describe, expect, test } from "bun:test"
import {
  findDrift,
  glossaryFile,
  modelVariants,
  parseTranslationArgs,
  runPool,
  sessionIDFromEvents,
  sessionModels,
  targetFiles,
  textFromEvents,
  translationConfig,
  unexpectedChanges,
} from "./translate-app"

describe("translate app", () => {
  test("parses one locale with the public model defaults", () => {
    expect(parseTranslationArgs(["fr"])).toEqual({
      target: "fr",
      concurrency: 1,
      model: "opencode/gpt-5.5",
      variant: "xhigh",
      dryRun: false,
      check: false,
      help: false,
    })
  })

  test("parses all locales with bounded concurrency overrides", () => {
    expect(
      parseTranslationArgs([
        "all",
        "--concurrency",
        "7",
        "--model",
        "opencode/gpt-5.4",
        "--variant",
        "high",
        "--dry-run",
      ]),
    ).toEqual({
      target: "all",
      concurrency: 7,
      model: "opencode/gpt-5.4",
      variant: "high",
      dryRun: true,
      check: false,
      help: false,
    })
  })

  test("rejects unsupported targets and invalid concurrency", () => {
    expect(() => parseTranslationArgs(["en"])).toThrow("Unknown locale")
    expect(() => parseTranslationArgs(["fr", "de"])).toThrow("one locale")
    expect(() => parseTranslationArgs(["all", "--concurrency", "0"])).toThrow("positive integer")
  })

  test("parses fresh-process parity checks without requesting translation", () => {
    expect(parseTranslationArgs(["fr", "--check"]).check).toBe(true)
  })

  test("limits each locale to its app surfaces", () => {
    expect(targetFiles("fr")).toEqual([
      "packages/app/src/i18n/fr.ts",
      "packages/ui/src/i18n/fr.ts",
      "packages/desktop/src/renderer/i18n/fr.ts",
    ])
    expect(targetFiles("tr")).toEqual([
      "packages/app/src/i18n/tr.ts",
      "packages/ui/src/i18n/tr.ts",
      "packages/desktop/src/renderer/i18n/tr.ts",
    ])
    expect(targetFiles("dv")).toEqual([
      "packages/app/src/i18n/dv.ts",
      "packages/ui/src/i18n/dv.ts",
      "packages/desktop/src/renderer/i18n/dv.ts",
    ])
  })

  test("maps product locale codes to their glossaries", () => {
    expect(glossaryFile("fr")).toBe(".opencode/glossary/fr.md")
    expect(glossaryFile("zh")).toBe(".opencode/glossary/zh-cn.md")
    expect(glossaryFile("zht")).toBe(".opencode/glossary/zh-tw.md")
  })

  test("finds key and placeholder drift", () => {
    expect(
      findDrift(
        { keep: "Hello {{name}}", missing: "Missing", changed: "{{one}} {{two}}" },
        { keep: "Bonjour {{name}}", extra: "Extra", changed: "{{one}}" },
      ),
    ).toEqual({ missing: ["missing"], extra: ["extra"], placeholders: ["changed"] })
  })

  test("accepts locale-specific CLDR plural variants", () => {
    expect(
      findDrift(
        { "files.one": "{{count}} file", "files.other": "{{count}} files" },
        {
          "files.one": "{{count}} ู…ู„ู",
          "files.two": "ู…ู„ูุงู†: {{count}}",
          "files.few": "{{count}} ู…ู„ูุงุช",
          "files.many": "{{count}} ู…ู„ูู‹ุง",
          "files.zero": "{{count}} ู…ู„ู",
          "files.other": "{{count}} ู…ู„ู",
        },
        "ar",
      ),
    ).toEqual({ missing: [], extra: [], placeholders: [] })
  })

  test("reports missing locale-specific CLDR plural variants", () => {
    const drift = findDrift(
      { "files.one": "{{count}} file", "files.other": "{{count}} files" },
      { "files.one": "{{count}} ู…ู„ู", "files.other": "{{count}} ู…ู„ู" },
      "ar",
    )
    expect(drift.missing).toContain("files.few")
  })

  test("reports placeholder drift in locale-specific CLDR plural variants", () => {
    const drift = findDrift(
      { "files.one": "{{count}} file", "files.other": "{{count}} files" },
      {
        "files.one": "{{count}} ู…ู„ู",
        "files.two": "ู…ู„ูุงู†: {{count}}",
        "files.few": "{{count}} ู…ู„ูุงุช",
        "files.many": "ู…ู„ูุงุช ูƒุซูŠุฑุฉ",
        "files.zero": "{{count}} ู…ู„ู",
        "files.other": "{{count}} ู…ู„ู",
      },
      "ar",
    )
    expect(drift.placeholders).toContain("files.many")
  })

  test("runs work with the requested maximum concurrency", async () => {
    const active = new Set<number>()
    const peaks: number[] = []
    const result = await runPool([1, 2, 3, 4, 5], 2, async (item) => {
      active.add(item)
      peaks.push(active.size)
      await Bun.sleep(5)
      active.delete(item)
      return item * 2
    })

    expect(result).toEqual([2, 4, 6, 8, 10])
    expect(Math.max(...peaks)).toBe(2)
  })

  test("reads the actual model and variant from the completed session", () => {
    expect(sessionIDFromEvents('shared: https://example.test\n{"type":"step_start","sessionID":"ses_test"}\n')).toBe(
      "ses_test",
    )
    expect(
      sessionModels({
        messages: [
          { info: { role: "user" } },
          {
            info: {
              role: "assistant",
              providerID: "opencode",
              modelID: "gpt-5.5",
              variant: "xhigh",
            },
          },
        ],
      }),
    ).toEqual([{ model: "opencode/gpt-5.5", variant: "xhigh" }])
    expect(
      textFromEvents(
        'shared: https://example.test\n{"type":"text","sessionID":"ses_test","part":{"text":"finished"}}\n',
      ),
    ).toBe("finished")
  })

  test("resolves variants from verbose model output", () => {
    const output = `opencode/other
{"variants":{}}
opencode/gpt-5.5
{"variants":{"high":{"reasoningEffort":"high"},"xhigh":{"reasoningEffort":"xhigh"}}}
opencode/next
{"variants":{}}
`
    expect(modelVariants(output, "opencode/gpt-5.5")).toEqual({
      high: { reasoningEffort: "high" },
      xhigh: { reasoningEffort: "xhigh" },
    })
  })

  test("disables side effects and scopes edits for the translation agent", () => {
    const config = translationConfig("translate-app-fr", "opencode/gpt-5.5", ["packages/app/src/i18n/fr.ts"])
    expect(config.share).toBe("disabled")
    expect(config.formatter).toBe(false)
    expect(config.lsp).toBe(false)
    expect(config.agent["translate-app-fr"].permission.webfetch).toBe("allow")
    expect(config.agent["translate-app-fr"].permission.websearch).toBe("allow")
    expect(config.agent["translate-app-fr"].permission.edit).toEqual({
      "*": "deny",
      "packages/app/src/i18n/fr.ts": "allow",
    })
  })

  test("detects edits outside the locale targets", () => {
    expect(
      unexpectedChanges(
        { "script/translate-app.ts": "before" },
        {
          "script/translate-app.ts": "before",
          "packages/app/src/i18n/fr.ts": "translated",
          "packages/app/src/app.tsx": "unexpected",
        },
        ["packages/app/src/i18n/fr.ts"],
      ),
    ).toEqual(["packages/app/src/app.tsx"])
    expect(unexpectedChanges({ "already-dirty.ts": "before" }, { "already-dirty.ts": "after" }, [])).toEqual([
      "already-dirty.ts",
    ])
  })
})