@@ -960,37 +960,57 @@ def complete(self, *, model: str, system: str, messages: Sequence[t.Message], ma
960960 assert run (capsys , root , "status" ) == (0 , "ja (日本語): 0 missing, 0 outdated, 4 current, 0 removable\n " , "" )
961961
962962
963- def test_rejected_credentials_with_pages_in_flight_start_no_further_page_but_a_finished_one_is_kept (
963+ class RejectedTogether :
964+ """Lets two requests in together, then rejects the credentials of those whose page title is in `rejected`."""
965+
966+ def __init__ (self , rejected : Sequence [str ]) -> None :
967+ self .rejected , self .calls = rejected , 0
968+ self .both , self .lock = threading .Barrier (2 , timeout = 5 ), threading .Lock ()
969+
970+ def complete (self , * , model : str , system : str , messages : Sequence [t .Message ], max_tokens : int ) -> t .Completion :
971+ with self .lock :
972+ self .calls += 1
973+ self .both .wait ()
974+ if any (title in messages [0 ].content for title in self .rejected ):
975+ raise t .ConfigError ("the API rejected the credentials: invalid bearer token" )
976+ return t .Completion (TOOLS_JA , t .Usage (10 , 4 , 0 , 9 ))
977+
978+
979+ def test_rejected_credentials_keep_a_page_that_lands_from_the_same_flight (
964980 tmp_path : Path , capsys : pytest .CaptureFixture [str ]
965981) -> None :
966- """Tool-defined: with `--jobs 2`, when the API rejects the credentials for one page while another is in
967- flight (here both are waiting before either is answered), the other page still lands and is written,
968- the two remaining pages are never requested, usage is reported, and the run stops with exit 2."""
982+ """Tool-defined: with two pages in flight together, the credentials being rejected for one does not throw
983+ away the other: whichever lands first, the good page is written and usage reported before exit 2."""
969984 root = make_repo (tmp_path )
985+ fake = RejectedTogether (["# Home" ])
970986
971- class RejectsHome :
972- def __init__ (self ) -> None :
973- self .both , self .lock , self .calls = threading .Barrier (2 , timeout = 5 ), threading .Lock (), 0
987+ code , out , err = translate (capsys , root , "--lang" , "ja" , "--pages" , "index.md" , "tools.md" , jobs = 2 , translator = fake )
988+
989+ assert (code , fake .calls , err ) == (2 , 2 , "translations: the API rejected the credentials: invalid bearer token\n " )
990+ assert out == snapshot ("""\
991+ translating 2 pages (ja), 2 at a time, with test-model
992+ ja: translated tools.md (3 of 3 sections)
993+ usage: 10 input / 4 output / 0 cache-write / 9 cache-read tokens
994+ """ )
995+ assert sorted (path .name for path in (root / "i18n" / "ja" / "pages" ).glob ("*.md" )) == ["tools.md" ]
974996
975- def complete (self , * , model : str , system : str , messages : Sequence [t .Message ], max_tokens : int ) -> t .Completion :
976- with self .lock :
977- self .calls += 1
978- self .both .wait ()
979- if "# Home" in messages [0 ].content :
980- raise t .ConfigError ("the API rejected the credentials: invalid bearer token" )
981- return t .Completion (TOOLS_JA , t .Usage (10 , 4 , 0 , 9 ))
982997
983- fake = RejectsHome ()
998+ def test_rejected_credentials_with_pages_in_flight_start_no_further_page (
999+ tmp_path : Path , capsys : pytest .CaptureFixture [str ]
1000+ ) -> None :
1001+ """Tool-defined: with `--jobs 2` over four pages, once the credentials are rejected (here for both pages in
1002+ flight) the two remaining pages are never requested and nothing is written."""
1003+ root = make_repo (tmp_path )
1004+ fake = RejectedTogether (["# Home" , "# Tools" ])
9841005
9851006 code , out , err = translate (capsys , root , "--lang" , "ja" , jobs = 2 , translator = fake )
9861007
9871008 assert (code , fake .calls , err ) == (2 , 2 , "translations: the API rejected the credentials: invalid bearer token\n " )
9881009 assert out == snapshot ("""\
9891010 translating 4 pages (ja), 2 at a time, with test-model
990- ja: translated tools.md (3 of 3 sections)
991- usage: 10 input / 4 output / 0 cache-write / 9 cache-read tokens
1011+ usage: 0 input / 0 output / 0 cache-write / 0 cache-read tokens
9921012""" )
993- assert sorted ( path . name for path in (root / "i18n" / "ja" / "pages" ).glob ( "*.md" )) == [ "tools.md" ]
1013+ assert not (root / "i18n" / "ja" / "pages" ).exists ()
9941014
9951015
9961016def test_translate_without_lang_works_through_every_language_with_that_languages_own_prompt (
0 commit comments