Raven-1/tests/test_design_engine_rendering_office.py
Tchen-data f178990780
fix: give the renderer a Han face rather than hoping the host has one (#668)
## Summary

Chinese in a deck came out as boxes (or as nothing) in the preview
panel, the delivery thumbnail, the template gallery, and the pages the
model renders to check its own work. LibreOffice reports success and the
PDF is well formed, so nothing flagged it.

Why, per platform:

- **Linux**: apt's `libreoffice` brings no CJK face.
- **macOS**: the system already ships Han faces (Songti, STHeiti,
Hiragino Sans GB, PingFang). Since 26.8 the macOS build renders headless
through a bundled fontconfig (`--enable-headless
--without-system-fontconfig` in `distro-configs/LibreOfficeMacOSX.conf`)
that looks for `/usr/local/etc/fonts/fonts.conf`, which an Apple Silicon
Mac does not have; without it that fontconfig lists 0 fonts, and a face
in `~/Library/Fonts` or a per-user fontconfig file is ignored. That is
why installing a font on a Mac never helped, and why the Macs that
worked had a CJK face copied into `LibreOffice.app` by hand.
openai/codex#32968 reports the same symptom from Codex's own
headless-only LibreOfficeDev build, whose fontconfig carries a
`/@..._OOO/etc/fonts` prefix; the cask build looks in
`/usr/local/etc/fonts`, so that thread's workaround is not the one
needed here.

What the PR does (the net diff; earlier commits on the branch tried
render-time detection, environment injection and then an
installer-written fontconfig file, and the later commits replace them):

1. **Linux font.** The LibreOffice offer installs `libreoffice
fonts-noto-cjk`. Where that did not run and `fc-list :lang=zh` is empty,
a pinned Noto Sans SC (size and sha256 checked before it is put in
place) goes into `~/.local/share/fonts` and `fc-cache` is rerun.
2. **macOS: the Mac's own faces, linked into LibreOffice's profile.**
LibreOffice always reads the `user/fonts` folder of the profile it runs
with. `office.link_han_faces(profile)` links Songti, STHeiti, Hiragino
Sans GB and PingFang (found under `AssetsV2` each time, so a link a
macOS update left dangling is replaced) into `profile/user/fonts`.
`office.to_pdf` does it for its throwaway profile (preview panel,
delivery thumbnail, template covers, ppt engine), the design engine for
its two profiles, and `ExecTool` for LibreOffice's default profile,
which the model's own `soffice --convert-to` uses. No download, no
password, nothing written outside the user's home, and it reaches users
who upgrade without rerunning `install.sh`.
3. **LibreOffice on a Mac without Homebrew.** This used to be a warning
and no preview at all. The installer now fetches the release dmg
(26.8.0, sha256 pinned per arch, from the stable directory and then the
archive), mounts it read-only, copies `LibreOffice.app` into
`/Applications` or `~/Applications`, never over an existing app, and
writes a two-line `~/.local/bin/soffice` launcher the way the cask does.
A failed `brew install` falls back to it too.
4. **Finding it.** `office.find_soffice` falls back to the launcher
inside `LibreOffice.app` in either Applications folder when nothing is
on PATH; the design engine now asks the same resolver.
5. **Stale renders.** The pdf-preview and deck-template cover cache keys
mix in `office.RENDER_GENERATION` (3), which retires every picture drawn
before this change. The Linux font step also clears both caches whenever
it installs a face.

Removed relative to earlier revisions: `raven/utils/fonts.py`
(`render_env`, `han_face`, `can_draw_han`, `host_han_faces`,
`install_hint`; several were never called), the exec-environment
wrapping, the ppt-engine face lookup, and the installer's sudo-written
`/usr/local/etc/fonts/fonts.conf`.

Known limits:

- A `soffice` run with a profile raven did not prepare (a caller passing
its own `-env:UserInstallation`) gets no faces on a Mac.
- A `deck.pdf` rendered beside a deck before the fix is still preferred
by the viewer until the deck is re-rendered or touched.
- Verified on macOS 15.7.1 arm64, and by review on macOS 26.5 arm64
(LibreOffice 26.8.0.3 from the cask); macOS 14 and Intel Macs have not
been run.

## Type

- [x] Fix
- [ ] Feature
- [ ] Docs
- [ ] CI / tooling
- [ ] Refactor
- [ ] Other

## Verification

macOS 15.7.1 arm64, LibreOffice 26.8.0.3 run from the stock dmg (no CJK
face in its bundle), fontconfig handed an empty configuration (so no
`fonts.conf` of any kind), HOME pointed at an empty directory:

- `main`: the model's own `soffice --convert-to` (default profile, after
building an `ExecTool`), `pdf_preview.pdf_for` / `png_for`, the ppt
engine's `to_pdf` and the design engine's `LibreOfficeBackend` each
embedded only Carlito and LinuxLibertineG (`pdffonts`).
- This branch: the same four paths each embedded PingFangSC, STHeitiSC
and STSongti-SC; the default profile held links to the five faces.
- No-Homebrew path, with PATH stripped of `/opt/homebrew`, an empty HOME
and a read-only Applications folder: the step downloaded the dmg,
matched its digest, installed into `~/Applications` (`codesign --verify
--deep --strict` valid, `spctl --assess` accepted) and wrote the
launcher; a second run downloaded nothing.
- Linux (container with every CJK face hidden): the font step downloaded
Noto Sans SC and the render embedded NotoSansSC; a second run did
nothing.
- `uv run --frozen --all-extras pytest -q tests/test_utils_office.py
tests/test_install_script.py
tests/test_design_engine_rendering_office.py
tests/test_rpc_deck_templates.py tests/test_shell_background.py
tests/test_ppt_engine_render_service.py`: 142 passed, 1 skipped.
- `uv run --frozen lint-imports`: 10 kept, 0 broken. `ruff check` /
`ruff format --check` on the changed files: clean. `shellcheck -s sh
install.sh`: no new findings. `scripts/check_source_language.py`: pass.

- [x] Relevant tests pass locally
- [x] Relevant lint / type checks pass locally
- [ ] User-facing docs or screenshots are updated when needed (no docs
describe this step)

## Risk

- [x] Security impact considered: the dmg and the font are checked
against pinned sha256 digests before use; the macOS change only creates
symlinks under LibreOffice profiles in the user's home, never replaces a
real file there, and runs no privileged command.
- [x] Backward compatibility considered: Linux hosts that already have a
CJK face are untouched; on a Mac, a profile that already holds its own
fonts keeps them. The cache-key bump costs one re-render.
- [x] Rollback path is clear for risky changes: deleting the links in
`~/Library/Application Support/LibreOffice/4/user/fonts` returns
LibreOffice to its previous behaviour; reverting the PR restores the old
installer.

User-visible: a Mac without Homebrew downloads about 300 MB of
LibreOffice once; nothing asks for a password.

## Related Issues

N/A

---------

Co-authored-by: litong <litong02@shanda.com>
Co-authored-by: Claude (claude-opus-4-6) <noreply@anthropic.com>
Co-authored-by: KT <74288668+0xKT@users.noreply.github.com>
2026-09-23 19:10:53 +08:00

179 lines
5.2 KiB
Python

"""Unit tests for Office renderer selection and fallback behavior."""
from __future__ import annotations
import subprocess
from pathlib import Path
from types import SimpleNamespace
import pytest
pytest.importorskip("fitz")
pytest.importorskip("PIL")
from raven_design.rendering.models import (
AdapterResult,
Detection,
RenderConfig,
RenderError,
RenderRequest,
)
from raven_design.rendering.office import LibreOfficeBackend, OfficeRouter
class _Backend:
def __init__(self, name: str, *, available: bool) -> None:
self.name = name
self._available = available
self.calls = 0
def available(self) -> bool:
return self._available
def render(self, source, bundle_root, detection, request):
self.calls += 1
return AdapterResult({}, {}, {}, {}, [], [])
def _detection(format: str = "docx") -> Detection:
return Detection(
format=format,
family="office",
mime="application/octet-stream",
declared_extension=f".{format}",
support_level="guaranteed",
metadata={},
)
def _request(source: Path, output: Path) -> RenderRequest:
return RenderRequest(path=source, output_dir=output)
def test_router_uses_first_available_configured_backend(tmp_path: Path) -> None:
onlyoffice = _Backend("onlyoffice", available=False)
libreoffice = _Backend("libreoffice", available=True)
router = OfficeRouter(
("onlyoffice", "libreoffice"),
{"onlyoffice": onlyoffice, "libreoffice": libreoffice},
)
result = router.render(
tmp_path / "input.docx",
tmp_path / "bundle",
_detection(),
_request(tmp_path / "input.docx", tmp_path),
)
assert isinstance(result, AdapterResult)
assert onlyoffice.calls == 0
assert libreoffice.calls == 1
def test_graph_backend_is_never_used_without_cloud_opt_in(tmp_path: Path) -> None:
graph = _Backend("graph", available=True)
router = OfficeRouter(("graph",), {"graph": graph}, allow_cloud=False)
with pytest.raises(RenderError) as raised:
router.render(
tmp_path / "input.docx",
tmp_path / "bundle",
_detection(),
_request(tmp_path / "input.docx", tmp_path),
)
assert raised.value.code == "renderer_unavailable"
assert graph.calls == 0
def test_libreoffice_false_success_without_pdf_is_rejected(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
source = tmp_path / "input.docx"
source.write_bytes(b"not-used")
config = RenderConfig(
chrome_path=None,
libreoffice_path="/usr/bin/libreoffice",
)
monkeypatch.setattr(
subprocess,
"run",
lambda *args, **kwargs: SimpleNamespace(
returncode=0,
stdout="convert complete",
stderr="",
),
)
with pytest.raises(RenderError) as raised:
LibreOfficeBackend(config).render(
source,
tmp_path / "bundle",
_detection(),
_request(source, tmp_path),
)
assert raised.value.code == "conversion_failed"
def test_libreoffice_timeout_is_retryable(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
source = tmp_path / "input.docx"
source.write_bytes(b"not-used")
config = RenderConfig(
chrome_path=None,
libreoffice_path="/usr/bin/libreoffice",
timeout_seconds=1,
)
def timeout(*args, **kwargs):
raise subprocess.TimeoutExpired("libreoffice", 1)
monkeypatch.setattr(subprocess, "run", timeout)
with pytest.raises(RenderError) as raised:
LibreOfficeBackend(config).render(
source,
tmp_path / "bundle",
_detection(),
_request(source, tmp_path),
)
assert raised.value.code == "render_timeout"
assert raised.value.retryable is True
def test_libreoffice_profile_carries_the_hosts_chinese_faces(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""The design engine's preview runs LibreOffice with a profile of its own;
on a Mac that profile is the only place the Chinese faces reach it from."""
from raven.utils import office
songti = tmp_path / "Songti.ttc"
songti.write_bytes(b"ttcf")
monkeypatch.setattr(office.sys, "platform", "darwin")
monkeypatch.setattr(office, "MACOS_SYSTEM_HAN_FACES", (songti,))
monkeypatch.setattr(office, "MACOS_ASSET_FONTS", tmp_path / "no-assets")
source = tmp_path / "input.pptx"
source.write_bytes(b"not-used")
config = RenderConfig(chrome_path=None, libreoffice_path="/usr/bin/libreoffice")
seen: list[str] = []
def run(command, **kwargs):
uri = next(token for token in command if token.startswith("-env:UserInstallation="))
profile = Path(uri.split("file://", 1)[1])
seen.extend(p.name for p in (profile / "user" / "fonts").iterdir())
return SimpleNamespace(returncode=0, stdout="", stderr="")
monkeypatch.setattr(subprocess, "run", run)
with pytest.raises(RenderError):
LibreOfficeBackend(config).render(source, tmp_path / "bundle", _detection("pptx"), _request(source, tmp_path))
assert seen == ["Songti.ttc"]