from pathlib import Path
from types import SimpleNamespace
from services import azure_static
class FakeContainer:
def __init__(self):
self.uploads = {}
self.existing = []
self.deleted = []
def upload_blob(self, name, data, **options):
self.uploads[name] = {"body": data.read(), **options}
def list_blobs(self, name_starts_with):
return [SimpleNamespace(name=name) for name in self.existing if name.startswith(name_starts_with)]
def delete_blob(self, name):
self.deleted.append(name)
def test_설정없으면_로컬발행만_유지(monkeypatch):
monkeypatch.delenv("AZURE_STORAGE_CONNECTION_STRING", raising=False)
assert azure_static.is_configured() is False
def test_ai_for_web_아래에_사이트와_공용산출물을_업로드(tmp_path, monkeypatch):
(tmp_path / "assets").mkdir()
(tmp_path / "assets" / "index-abc.js").write_text("console.log(1)")
(tmp_path / "fonts").mkdir()
(tmp_path / "fonts" / "Pretendard.woff2").write_bytes(b"font")
# ★ 오리진 루트 파일 — 크롤러가 robots.txt 를 읽는 유일한 자리다(RFC 9309).
(tmp_path / "robots.txt").write_text("User-agent: *\nAllow: /\n")
(tmp_path / "sitemap.xml").write_text("")
site_dir = tmp_path / "s" / "butter"
(site_dir / "about").mkdir(parents=True)
(site_dir / "index.html").write_text("
Butter
")
(site_dir / "about" / "index.html").write_text("About
")
# 다른 사업장의 발행본. 이번 발행 대상이 아니므로 올라가면 안 된다.
(tmp_path / "s" / "joy").mkdir(parents=True)
(tmp_path / "s" / "joy" / "index.html").write_text("Joy
")
container = FakeContainer()
container.existing = [
"ai-for-web/s/butter/index.html",
"ai-for-web/s/butter/old/index.html",
]
service = SimpleNamespace(get_container_client=lambda name: container)
monkeypatch.setattr(
azure_static.BlobServiceClient,
"from_connection_string",
lambda value: service,
)
monkeypatch.setattr(azure_static, "output_dir", lambda: Path(tmp_path))
monkeypatch.setenv("AZURE_STORAGE_CONNECTION_STRING", "UseDevelopmentStorage=true")
monkeypatch.delenv("AZURE_STORAGE_PREFIX", raising=False)
result = azure_static._publish_sync("butter")
assert set(container.uploads) == {
"ai-for-web/assets/index-abc.js",
"ai-for-web/fonts/Pretendard.woff2",
"ai-for-web/robots.txt",
"ai-for-web/sitemap.xml",
"ai-for-web/s/butter/index.html",
"ai-for-web/s/butter/about/index.html",
}
assert container.deleted == ["ai-for-web/s/butter/old/index.html"]
assert result == {
"container": "$web",
"prefix": "ai-for-web",
"shared_files": 4,
"site_files": 2,
"removed_stale": 1,
}
def test_루트_robots_와_사이트맵은_짧게_캐시된다():
# 해시가 박힌 번들만 영구 캐시다. robots·사이트맵이 길게 캐시되면
# 새 사업장이 사이트맵 인덱스에 들어가도 크롤러가 옛 파일을 계속 본다.
assert "immutable" in azure_static._cache_control(Path("assets/index-abc.js"))
assert azure_static._cache_control(Path("robots.txt")) == "public, max-age=60, must-revalidate"
assert azure_static._cache_control(Path("sitemap.xml")) == "public, max-age=60, must-revalidate"
assert azure_static._cache_control(Path("s/butter/index.html")) == "public, max-age=60, must-revalidate"
assert azure_static._cache_control(Path("fonts/Pretendard.woff2")) == "public, max-age=604800"