mirror of
https://github.com/safishamsi/graphify.git
synced 2026-09-25 06:55:43 +00:00
Add optional Google Workspace shortcut export
This commit is contained in:
@@ -258,6 +258,40 @@ def test_classify_video_extensions():
|
||||
assert classify_file(Path("audio.m4a")) == FileType.VIDEO
|
||||
|
||||
|
||||
def test_classify_google_workspace_shortcuts():
|
||||
assert classify_file(Path("notes.gdoc")) == FileType.DOCUMENT
|
||||
assert classify_file(Path("budget.gsheet")) == FileType.DOCUMENT
|
||||
assert classify_file(Path("deck.gslides")) == FileType.DOCUMENT
|
||||
|
||||
|
||||
def test_detect_skips_google_workspace_shortcuts_by_default(tmp_path):
|
||||
(tmp_path / "notes.gdoc").write_text('{"doc_id":"doc-1"}', encoding="utf-8")
|
||||
|
||||
result = detect(tmp_path)
|
||||
|
||||
assert not result["files"]["document"]
|
||||
assert any("Google Workspace shortcut skipped" in item for item in result["skipped_sensitive"])
|
||||
|
||||
|
||||
def test_detect_converts_google_workspace_shortcuts_when_enabled(tmp_path, monkeypatch):
|
||||
shortcut = tmp_path / "notes.gdoc"
|
||||
shortcut.write_text('{"doc_id":"doc-1"}', encoding="utf-8")
|
||||
|
||||
def fake_convert(path, out_dir, *, xlsx_to_markdown=None):
|
||||
out_dir.mkdir(parents=True, exist_ok=True)
|
||||
out = out_dir / "notes_converted.md"
|
||||
out.write_text("# Notes\n\nA converted Google Doc.", encoding="utf-8")
|
||||
return out
|
||||
|
||||
monkeypatch.setattr("graphify.detect.convert_google_workspace_file", fake_convert)
|
||||
|
||||
result = detect(tmp_path, google_workspace=True)
|
||||
|
||||
assert len(result["files"]["document"]) == 1
|
||||
assert result["files"]["document"][0].endswith("notes_converted.md")
|
||||
assert result["total_words"] > 0
|
||||
|
||||
|
||||
def test_detect_includes_video_key(tmp_path):
|
||||
"""detect() result always includes a 'video' key even with no video files."""
|
||||
(tmp_path / "main.py").write_text("x = 1")
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
from pathlib import Path
|
||||
|
||||
import graphify.google_workspace as gw
|
||||
|
||||
|
||||
def test_read_google_shortcut_doc_id(tmp_path):
|
||||
shortcut = tmp_path / "Planning.gdoc"
|
||||
shortcut.write_text(
|
||||
'{"url":"https://docs.google.com/document/d/doc-123/edit","doc_id":"doc-123","email":"me@example.com"}',
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
metadata = gw.read_google_shortcut(shortcut)
|
||||
|
||||
assert metadata["file_id"] == "doc-123"
|
||||
assert metadata["account"] == "me@example.com"
|
||||
|
||||
|
||||
def test_read_google_shortcut_extracts_id_from_url(tmp_path):
|
||||
shortcut = tmp_path / "Budget.gsheet"
|
||||
shortcut.write_text(
|
||||
'{"url":"https://docs.google.com/spreadsheets/d/sheet-456/edit?resourcekey=key-1"}',
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
metadata = gw.read_google_shortcut(shortcut)
|
||||
|
||||
assert metadata["file_id"] == "sheet-456"
|
||||
assert metadata["resource_key"] == "key-1"
|
||||
|
||||
|
||||
def test_convert_gdoc_to_markdown_sidecar(tmp_path, monkeypatch):
|
||||
shortcut = tmp_path / "Planning.gdoc"
|
||||
shortcut.write_text(
|
||||
'{"url":"https://docs.google.com/document/d/doc-123/edit","doc_id":"doc-123"}',
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
def fake_export(file_id, mime_type, output, resource_key=None):
|
||||
assert file_id == "doc-123"
|
||||
assert mime_type == "text/markdown"
|
||||
output.write_text("# Planning\n\nExported doc text.", encoding="utf-8")
|
||||
|
||||
monkeypatch.setattr(gw, "_run_gws_export", fake_export)
|
||||
|
||||
out = gw.convert_google_workspace_file(shortcut, tmp_path / "converted")
|
||||
|
||||
assert out is not None
|
||||
assert out.suffix == ".md"
|
||||
content = out.read_text(encoding="utf-8")
|
||||
assert 'source_type: "google_workspace"' in content
|
||||
assert "# Planning" in content
|
||||
|
||||
|
||||
def test_convert_gsheet_uses_xlsx_markdown_callback(tmp_path, monkeypatch):
|
||||
shortcut = tmp_path / "Budget.gsheet"
|
||||
shortcut.write_text('{"doc_id":"sheet-456"}', encoding="utf-8")
|
||||
|
||||
def fake_export(file_id, mime_type, output, resource_key=None):
|
||||
assert file_id == "sheet-456"
|
||||
assert mime_type == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
||||
output.write_bytes(b"xlsx")
|
||||
|
||||
monkeypatch.setattr(gw, "_run_gws_export", fake_export)
|
||||
|
||||
out = gw.convert_google_workspace_file(
|
||||
shortcut,
|
||||
tmp_path / "converted",
|
||||
xlsx_to_markdown=lambda path: "## Sheet: Main\n\n| A |\n| --- |\n| 1 |",
|
||||
)
|
||||
|
||||
assert out is not None
|
||||
assert "## Sheet: Main" in out.read_text(encoding="utf-8")
|
||||
|
||||
|
||||
def test_google_workspace_enabled_env(monkeypatch):
|
||||
monkeypatch.setenv("GRAPHIFY_GOOGLE_WORKSPACE", "yes")
|
||||
assert gw.google_workspace_enabled()
|
||||
|
||||
monkeypatch.setenv("GRAPHIFY_GOOGLE_WORKSPACE", "0")
|
||||
assert not gw.google_workspace_enabled()
|
||||
Reference in New Issue
Block a user