File size: 8,331 Bytes
54c3e65
 
 
 
f11438f
54c3e65
f11438f
54c3e65
 
f11438f
54c3e65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f11438f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
from __future__ import annotations

import io
import json
import sqlite3

import deberta_ime.cli as cli_module
from deberta_ime import RerankRequest
from deberta_ime.cli import run
from deberta_ime.mozc import build_mozc_index


class StaticScorer:
    def score_candidates(self, request: RerankRequest) -> list[float]:
        return [-3.0, -0.5]


def test_jsonl_cli_returns_ranked_candidates_and_decision_metadata() -> None:
    stdin = io.StringIO(
        json.dumps(
            {
                "reading": "はし",
                "left_context": ["川", "に", "架かる"],
                "right_context": ["を", "渡る"],
                "candidates": [
                    {"surface": "箸", "prior_score": 0.0},
                    {"surface": "橋", "prior_score": 0.0},
                ],
            },
            ensure_ascii=False,
        )
    )
    stdout = io.StringIO()

    exit_code = run(
        ["rerank", "--prior-weight", "0", "--min-margin", "0.5"],
        stdin=stdin,
        stdout=stdout,
        scorer=StaticScorer(),
    )

    output = json.loads(stdout.getvalue())
    assert exit_code == 0
    assert output["ok"] is True
    assert output["decision"] == {
        "changed": True,
        "reason": "accepted",
        "margin": 2.5,
    }
    assert output["ranked"][0] == {
        "surface": "橋",
        "original_rank": 1,
        "prior_score": 0.0,
        "model_score": -0.5,
        "combined_score": -0.5,
    }


def test_cli_reads_and_writes_utf8_files_without_console_encoding(tmp_path) -> None:
    input_path = tmp_path / "request.jsonl"
    output_path = tmp_path / "response.jsonl"
    input_path.write_text(
        json.dumps(
            {
                "reading": "はし",
                "candidates": ["箸", "橋"],
            },
            ensure_ascii=False,
        ),
        encoding="utf-8",
    )

    exit_code = run(
        [
            "rerank",
            "--input",
            str(input_path),
            "--output",
            str(output_path),
            "--prior-weight",
            "0",
        ],
        scorer=StaticScorer(),
    )

    output = json.loads(output_path.read_text(encoding="utf-8"))
    assert exit_code == 0
    assert output["reading"] == "はし"
    assert output["ranked"][0]["surface"] == "橋"


def test_cli_rejects_same_input_and_output_without_truncating(tmp_path) -> None:
    path = tmp_path / "request.jsonl"
    original = json.dumps({"reading": "はし", "candidates": ["箸", "橋"]})
    path.write_text(original, encoding="utf-8")
    stderr = io.StringIO()

    exit_code = run(
        ["rerank", "--input", str(path), "--output", str(path)],
        stderr=stderr,
        scorer=StaticScorer(),
    )

    assert exit_code == 2
    assert "must be different" in stderr.getvalue()
    assert path.read_text(encoding="utf-8") == original


def test_mozc_index_cli_builds_index_and_prints_source_manifest(tmp_path) -> None:
    dictionary_dir = tmp_path / "dictionary_oss"
    dictionary_dir.mkdir()
    (dictionary_dir / "dictionary00.txt").write_text(
        "はし\t1\t1\t3500\t橋\n",
        encoding="utf-8",
    )
    index_path = tmp_path / "mozc.sqlite3"
    stdout = io.StringIO()

    exit_code = run(
        [
            "mozc-index",
            "--dictionary-dir",
            str(dictionary_dir),
            "--output",
            str(index_path),
            "--source-revision",
            "fixture-revision",
        ],
        stdout=stdout,
    )

    payload = json.loads(stdout.getvalue())
    assert exit_code == 0
    assert index_path.is_file()
    assert payload["ok"] is True
    assert payload["manifest"]["source_revision"] == "fixture-revision"
    assert payload["manifest"]["indexed_entries"] == 1


def test_mozc_index_cli_reports_invalid_source_without_traceback(tmp_path) -> None:
    stdout = io.StringIO()
    stderr = io.StringIO()

    exit_code = run(
        [
            "mozc-index",
            "--dictionary-dir",
            str(tmp_path / "missing"),
            "--output",
            str(tmp_path / "mozc.sqlite3"),
        ],
        stdout=stdout,
        stderr=stderr,
    )

    assert exit_code == 2
    assert stdout.getvalue() == ""
    assert stderr.getvalue() == "error: no dictionary??.txt files found\n"


def test_mozc_index_cli_reports_sqlite_failure_without_traceback(tmp_path, monkeypatch) -> None:
    def fail_build(*args: object, **kwargs: object) -> None:
        del args, kwargs
        raise sqlite3.OperationalError("fixture database failure")

    monkeypatch.setattr(cli_module, "build_mozc_index", fail_build)
    stdout = io.StringIO()
    stderr = io.StringIO()

    exit_code = run(
        [
            "mozc-index",
            "--dictionary-dir",
            str(tmp_path / "dictionary"),
            "--output",
            str(tmp_path / "mozc.sqlite3"),
        ],
        stdout=stdout,
        stderr=stderr,
    )

    assert exit_code == 2
    assert stdout.getvalue() == ""
    assert stderr.getvalue() == "error: fixture database failure\n"


def test_mozc_rerank_cli_generates_finite_candidates_before_reranking(tmp_path) -> None:
    dictionary_dir = tmp_path / "dictionary_oss"
    dictionary_dir.mkdir()
    (dictionary_dir / "dictionary00.txt").write_text(
        "はし\t1\t1\t3500\t箸\nはし\t1\t1\t3800\t橋\n",
        encoding="utf-8",
    )
    index_path = tmp_path / "mozc.sqlite3"
    build_mozc_index(dictionary_dir, index_path, source_revision="fixture")
    stdin = io.StringIO(
        json.dumps(
            {
                "reading": "ハシ",
                "left_context": ["川", "に", "架かる"],
                "right_context": ["を", "渡る"],
            },
            ensure_ascii=False,
        )
    )
    stdout = io.StringIO()

    class StrongContextScorer:
        def score_candidates(self, request: RerankRequest) -> list[float]:
            return [-10.0, 0.0]

    exit_code = run(
        [
            "mozc-rerank",
            "--index",
            str(index_path),
            "--profile",
            "bidirectional",
        ],
        stdin=stdin,
        stdout=stdout,
        scorer=StrongContextScorer(),
    )

    payload = json.loads(stdout.getvalue())
    assert exit_code == 0
    assert payload["candidate_source"] == {
        "kind": "mozc_oss_dictionary_index",
        "source_revision": "fixture",
        "limit": 8,
    }
    assert payload["profile"] == "bidirectional"
    assert payload["profile_config"] == {
        "prior_weight": 3.0,
        "min_margin": 1.5,
        "selected_on_revision": "7bc20119f476b552635e0640644e577b6fd3606b",
    }
    assert [item["surface"] for item in payload["ranked"]] == ["橋", "箸"]
    assert payload["decision"]["changed"] is True


def test_incremental_profile_rejects_future_context_instead_of_leaking_it(tmp_path) -> None:
    dictionary_dir = tmp_path / "dictionary_oss"
    dictionary_dir.mkdir()
    (dictionary_dir / "dictionary00.txt").write_text(
        "はし\t1\t1\t3500\t箸\nはし\t1\t1\t3800\t橋\n",
        encoding="utf-8",
    )
    index_path = tmp_path / "mozc.sqlite3"
    build_mozc_index(dictionary_dir, index_path, source_revision="fixture")
    stdin = io.StringIO(
        json.dumps(
            {"reading": "はし", "left_context": ["川"], "right_context": ["を"]},
            ensure_ascii=False,
        )
    )
    stdout = io.StringIO()

    exit_code = run(
        ["mozc-rerank", "--index", str(index_path), "--profile", "incremental"],
        stdin=stdin,
        stdout=stdout,
        scorer=StaticScorer(),
    )

    payload = json.loads(stdout.getvalue())
    assert exit_code == 2
    assert payload["ok"] is False
    assert payload["error"] == "incremental profile does not accept right_context"


def test_mozc_rerank_cli_rejects_corrupt_index_before_reading_requests(tmp_path) -> None:
    index_path = tmp_path / "corrupt.sqlite3"
    index_path.write_bytes(b"not a sqlite database")
    stderr = io.StringIO()

    exit_code = run(
        ["mozc-rerank", "--index", str(index_path)],
        stdin=io.StringIO(""),
        stdout=io.StringIO(),
        stderr=stderr,
        scorer=StaticScorer(),
    )

    assert exit_code == 2
    assert stderr.getvalue().startswith("error: cannot open Mozc index: ")