blob: 1141efbad8c428ee85aed6ccb2060c256b6faaa4 [file]
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
"""Help markdown renders inside the page frame."""
import io
import pathlib
import asfquart
import ezt
from easydict import EasyDict as edict # noqa: N813
THIS_DIR = pathlib.Path(__file__).resolve().parent.parent
def _pages():
if asfquart.APP is None:
asfquart.construct(
"llmao", app_dir=str(THIS_DIR), cfg_file="config.yaml.example", oauth=False, force_login=False
)
import pages
return pages
def test_article_renders_markdown_and_raw_html(tmp_path):
(tmp_path / "index.md").write_text("# Overview\n\nHello <em>there</em>\n", encoding="utf-8")
(tmp_path / "faq.md").write_text("# FAQ\n\nQuestions\n", encoding="utf-8")
doc = _pages().help_article("index", tmp_path)
assert doc.title == "Overview"
assert "<em>there</em>" in doc.html
assert "<h1>" in doc.html
titles = [item.title for item in _pages().help_articles(tmp_path)]
assert titles == ["FAQ"]
def test_unknown_slug_is_missing(tmp_path):
assert _pages().help_article("../secret", tmp_path) is None
assert _pages().help_article("missing", tmp_path) is None
def test_help_template_uses_the_page_frame(tmp_path):
(tmp_path / "index.md").write_text("# Overview\n\nBody text\n", encoding="utf-8")
doc = _pages().help_article("index", tmp_path)
data = edict(
title=doc.title,
body=doc.html,
guides=[edict(slug="pi", title="Pi")],
flashes=[],
work=None,
notices=[],
uid=None,
name=None,
is_site_admin=ezt.boolean(False),
admin_at_heart=ezt.boolean(False),
as_user=ezt.boolean(False),
nav="help",
login_back="/help",
restarted_at="",
repo="https://example.test/llmao",
commit="abc",
can_create_automation=ezt.boolean(False),
)
template = ezt.Template(str(THIS_DIR / "templates" / "help.ezt"), base_format=ezt.FORMAT_HTML)
buf = io.StringIO()
template.generate(buf, data)
out = buf.getvalue()
assert "<h1>Overview</h1>" in out
assert "&lt;h1&gt;" not in out
assert "Body text" in out
assert 'href="/help"' in out
# The bar marks the page you are on, and only that one.
assert 'class="nav-link active" href="/help" aria-current="page"' in out
assert out.count('aria-current="page"') == 1
# Signing in from here comes back here.
assert 'href="/auth?login=/help"' in out
assert "Apache LLMAO" in out
# Help is a utility: once in the bar, after the working pages.
assert out.count(">Help</a>") == 1
# The links the ASF asks project sites to carry in their navigation.
for text, url in (
("License", "https://www.apache.org/licenses/"),
("Sponsorship", "https://www.apache.org/foundation/sponsorship.html"),
("Thanks", "https://www.apache.org/foundation/thanks.html"),
("Security", "https://www.apache.org/security/"),
("Privacy", "https://privacy.apache.org/policies/privacy-policy-public.html"),
):
assert f'<a class="dropdown-item" href="{url}">{text}</a>' in out
assert 'href="https://www.apache.org/"' in out
footer = out[out.index("<footer") :]
assert "registered trademarks or" in footer
assert '<a href="https://www.apache.org/">The Apache Software Foundation</a>' in footer
assert "™" not in out and "&trade;" not in out
assert 'href="/help/faq"' in out
# The terms are linked beside the privacy policy, not listed as a guide.
assert 'href="/help/terms">Terms of Use</a>' in footer
def test_terms_page_names_the_running_commit(tmp_path):
"""An article may name the commit it is served from, and nothing else of basic_info."""
(tmp_path / "terms.md").write_text("# Terms\n\nFrom [[]commit [commit]]([repo]/blob/[commit]/x.md)\n")
pages = _pages()
doc = pages.help_article("terms", tmp_path)
assert f"commit {pages.COMMIT}" in doc.html
assert f'href="{pages.REPO_URL}/blob/{pages.COMMIT}/x.md"' in doc.html
def test_terms_are_a_help_page_but_not_a_guide():
pages = _pages()
assert "terms" in pages.HELP_UNLISTED
doc = pages.help_article("terms")
assert "<h1>Terms of Use</h1>" in doc.html
for text in ("Private", "Approximately 90 days", "rai.apache.org", f"commit {pages.COMMIT}"):
assert text in doc.html
assert "terms" not in [g.slug for g in pages.help_articles() if g.slug not in pages.HELP_UNLISTED]
def test_index_links_each_tool_page():
help_dir = THIS_DIR / "docs" / "help"
index = (help_dir / "index.md").read_text(encoding="utf-8")
for slug in ("claude-code", "codex", "grok-build", "pi"):
assert f"/help/{slug}" in index
assert (help_dir / f"{slug}.md").is_file()
faq = (help_dir / "faq.md").read_text(encoding="utf-8")
assert "/help/claude-code" in faq
prose = "\n".join(path.read_text(encoding="utf-8") for path in help_dir.glob("*.md"))
assert "LiteLLM" not in prose
assert "Anthropic" not in prose
assert "/keys" in prose
assert "/models" in prose
signed_out = _pages().help_article("index")
signed_in = _pages().help_article("index", uid="alice")
assert 'href="/auth?login=/"' in signed_out.html
assert "/auth?login=/" not in signed_in.html
assert 'href="/keys"' in signed_in.html
def test_nav_section_follows_the_first_path_segment():
pages = _pages()
assert pages._nav_section("/keys/new") == "keys"
assert pages._nav_section("/help/faq") == "help"
assert pages._nav_section("/") == ""
assert pages._nav_section("/do-add-deployment") == ""
def test_home_page_does_not_list_api_paths():
text = (THIS_DIR / "templates" / "home.ezt").read_text(encoding="utf-8")
assert "/v1/projects" not in text
assert "/healthz" not in text
assert "LiteLLM" not in text
assert "llm.apache.org" in text
assert "PAT" not in text
def _frame(**extra):
data = dict(
title="t",
flashes=[],
work=None,
notices=[],
uid=None,
name=None,
projects=[],
projects_label=None,
is_site_admin=ezt.boolean(False),
admin_at_heart=ezt.boolean(False),
as_user=ezt.boolean(False),
nav="",
login_back="/",
restarted_at="",
repo="https://example.test/llmao",
commit="abc",
can_create_automation=ezt.boolean(False),
)
data.update(extra)
return edict(data)
def _generate(name: str, data) -> str:
template = ezt.Template(str(THIS_DIR / "templates" / name), base_format=ezt.FORMAT_HTML)
buf = io.StringIO()
template.generate(buf, data)
return buf.getvalue()
def _steps(key=False, tool=False):
"""The home page's step state, as home_page() hands it to the template."""
return dict(key_step_done=ezt.boolean(key), tool_step_done=ezt.boolean(tool))
def test_home_page_is_the_steps_to_a_first_request():
signed_out = _generate("home.ezt", _frame(**_steps()))
assert signed_out.count('<li class="home-step') == 3
assert "home-step-done" not in signed_out
assert 'href="/auth?login=/"' in signed_out
assert 'href="/help"' in signed_out
assert "/keys/new" not in signed_out
# Signed in, nothing else: the key is the step to take.
alice = dict(uid="alice", name="Alice", projects_label="infra")
fresh = _generate("home.ezt", _frame(**alice, **_steps()))
assert fresh.count("home-step-done") == 1
assert "Signed in as" in fresh
assert 'btn btn-primary btn-sm" href="/keys/new"' in fresh
# A key but no request yet.
keyed = _generate("home.ezt", _frame(**alice, **_steps(key=True)))
assert keyed.count("home-step-done") == 2
assert "You have a key" in keyed
assert 'btn btn-outline-primary btn-sm" href="/keys"' in keyed
assert "/keys/new" not in keyed
for href in ("/help", "/models"):
assert f'btn-sm" href="{href}"' in keyed
# Everything done: three checks, nothing left to press.
done = _generate("home.ezt", _frame(**alice, **_steps(key=True, tool=True)))
assert done.count("home-step-done") == 3
assert "requests are flowing" in done
assert "btn btn-primary " not in done
def test_create_key_form_preselects_a_given_project():
common = dict(uid="alice", name="Alice", form_purpose="", for_model="", has_for_model=ezt.boolean(False))
one = _generate("key_create.ezt", _frame(projects=[edict(name="infra")], form_project="infra", **common))
assert '<option value="infra" selected>infra</option>' in one
two = _generate(
"key_create.ezt",
_frame(projects=[edict(name="infra"), edict(name="tooling")], form_project="", **common),
)
assert " selected>" not in two