directory name fix
This commit is contained in:
1 parent
b862e21b99
commit
190079e84a
108 files changed
+1609
-261
No files matched your search
@@ -6,7 +6,7 @@ from pathlib import Path
|
||||
|
||||
from twitter.emit_archive import missing_local_media, preserve_review_state
|
||||
from twitter.media_manifest import new_original_item
|
||||
from twitter.render import render_media
|
||||
from twitter.render import render_media, title_text
|
||||
|
||||
|
||||
class EmitMediaTests(unittest.TestCase):
|
||||
@@ -62,6 +62,13 @@ class EmitMediaTests(unittest.TestCase):
|
||||
)
|
||||
self.assertEqual(missing_local_media([item]), ["100/AAA/orig"])
|
||||
|
||||
def test_title_text_is_shared_by_frontmatter_and_slug(self) -> None:
|
||||
self.assertEqual(
|
||||
title_text("Root title. Extra first-line detail"),
|
||||
"Root title.",
|
||||
)
|
||||
self.assertEqual(title_text("One uninterrupted title"), "One uninterrupted title")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -13,6 +13,7 @@ import shutil
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from tempfile import TemporaryDirectory
|
||||
|
||||
from twitter.emit_archive import emit
|
||||
from twitter.models import PostData, PostMetrics, ThreadData
|
||||
@@ -812,5 +813,388 @@ class TipIsOpTests(unittest.TestCase):
|
||||
self.assertEqual(spine, ["2000", "2001", "2002"])
|
||||
|
||||
|
||||
class ReslugReuseTests(unittest.TestCase):
|
||||
"""Compact same-handle integration tests for the emit-side rename flow.
|
||||
|
||||
Each test pre-populates one or more archive/asset pairs under the
|
||||
vault, invokes ``emit`` with explicit ``reconcile_scratch`` and
|
||||
``frozen_ids`` so nothing is written to the workspace scratch and
|
||||
no frozen list is read from secrets.
|
||||
"""
|
||||
|
||||
HANDLE = "alice"
|
||||
ROOT_ID = "1000"
|
||||
TIMESTAMP = "2026-08-24 12:00:00"
|
||||
ROOT_TEXT = "Root title. Extra first-line detail that should be dropped."
|
||||
EXPECTED_CANONICAL = "2026-08-24-root-title"
|
||||
|
||||
def setUp(self) -> None:
|
||||
self.tmp = Path(tempfile.mkdtemp(prefix="reslug_reuse_"))
|
||||
self.input_dir = self.tmp / "input"
|
||||
self.input_dir.mkdir()
|
||||
self.vault = self.tmp / "vault"
|
||||
(self.vault / "archive" / "threads").mkdir(parents=True)
|
||||
(self.vault / "assets" / "threads").mkdir(parents=True)
|
||||
|
||||
def tearDown(self) -> None:
|
||||
shutil.rmtree(self.tmp, ignore_errors=True)
|
||||
|
||||
def _write_thread(self) -> None:
|
||||
thread = {
|
||||
"root_post_id": self.ROOT_ID,
|
||||
"source_url": f"https://x.com/{self.HANDLE}/status/{self.ROOT_ID}",
|
||||
"posts": [
|
||||
{
|
||||
"post_id": self.ROOT_ID,
|
||||
"author": self.HANDLE,
|
||||
"handle": self.HANDLE,
|
||||
"text": self.ROOT_TEXT,
|
||||
"timestamp": self.TIMESTAMP,
|
||||
"media_urls": [],
|
||||
"reply_to_id": None,
|
||||
"quote_of_id": None,
|
||||
"metrics": {
|
||||
"reply_count": 0,
|
||||
"repost_count": 0,
|
||||
"like_count": 0,
|
||||
"view_count": 0,
|
||||
},
|
||||
}
|
||||
],
|
||||
}
|
||||
(self.input_dir / "thread_data.json").write_text(
|
||||
json.dumps(thread, indent=2),
|
||||
encoding="utf-8",
|
||||
newline="\n",
|
||||
)
|
||||
|
||||
def _write_existing_pair(
|
||||
self,
|
||||
*,
|
||||
dir_name: str,
|
||||
post_id: str,
|
||||
index_body: str = "",
|
||||
editorial_tag: str | None = None,
|
||||
draft: str = "true",
|
||||
timestamp: str | None = None,
|
||||
) -> tuple[Path, Path]:
|
||||
archive = self.vault / "archive" / "threads" / self.HANDLE / dir_name
|
||||
asset = self.vault / "assets" / "threads" / self.HANDLE / dir_name
|
||||
archive.mkdir(parents=True)
|
||||
asset.mkdir(parents=True)
|
||||
body_timestamp = timestamp or self.TIMESTAMP
|
||||
date_value = body_timestamp[:10]
|
||||
tags_lines = (
|
||||
"tags:\n"
|
||||
" - archive\n"
|
||||
" - twitter\n"
|
||||
f" - {self.HANDLE}\n"
|
||||
)
|
||||
if editorial_tag:
|
||||
tags_lines += f" - {editorial_tag}\n"
|
||||
(archive / "index.md").write_text(
|
||||
"---\n"
|
||||
'title: "Old title."\n'
|
||||
f"handle: {self.HANDLE}\n"
|
||||
f'post_id: "{post_id}"\n'
|
||||
f"date: {date_value}\n"
|
||||
f"draft: {draft}\n"
|
||||
f"{tags_lines}---\n\n{index_body}",
|
||||
encoding="utf-8",
|
||||
newline="\n",
|
||||
)
|
||||
(asset / "thread_data.json").write_text(
|
||||
json.dumps(
|
||||
{
|
||||
"root_post_id": post_id,
|
||||
"source_url": f"https://x.com/{self.HANDLE}/status/{post_id}",
|
||||
"posts": [
|
||||
{
|
||||
"post_id": post_id,
|
||||
"author": self.HANDLE,
|
||||
"handle": self.HANDLE,
|
||||
"text": "Old body.",
|
||||
"timestamp": body_timestamp,
|
||||
"media_urls": [],
|
||||
"reply_to_id": None,
|
||||
"quote_of_id": None,
|
||||
"metrics": {},
|
||||
}
|
||||
],
|
||||
},
|
||||
indent=2,
|
||||
),
|
||||
encoding="utf-8",
|
||||
newline="\n",
|
||||
)
|
||||
(asset / "media.json").write_text(
|
||||
json.dumps(
|
||||
{
|
||||
"schema_version": 2,
|
||||
"root_post_id": post_id,
|
||||
"items": [],
|
||||
"mirrors": [],
|
||||
},
|
||||
indent=2,
|
||||
),
|
||||
encoding="utf-8",
|
||||
newline="\n",
|
||||
)
|
||||
return archive, asset
|
||||
|
||||
def test_emit_renames_reused_pair_to_canonical_dir(self) -> None:
|
||||
"""Existing archive/asset pair found by post identity is renamed
|
||||
to date + rendered-title canonical directory."""
|
||||
old_dir = "2026-08-24-stale-slug"
|
||||
archive_old, asset_old = self._write_existing_pair(
|
||||
dir_name=old_dir,
|
||||
post_id=self.ROOT_ID,
|
||||
)
|
||||
self._write_thread()
|
||||
|
||||
with TemporaryDirectory() as scratch_raw:
|
||||
emit(
|
||||
input_dir=self.input_dir,
|
||||
vault=self.vault,
|
||||
slug=None,
|
||||
archived="2026-08-24",
|
||||
force=True,
|
||||
reconcile_scratch=Path(scratch_raw),
|
||||
frozen_ids=set(),
|
||||
)
|
||||
|
||||
self.assertFalse(archive_old.exists())
|
||||
self.assertFalse(asset_old.exists())
|
||||
archive_new = (
|
||||
self.vault
|
||||
/ "archive"
|
||||
/ "threads"
|
||||
/ self.HANDLE
|
||||
/ self.EXPECTED_CANONICAL
|
||||
)
|
||||
asset_new = (
|
||||
self.vault
|
||||
/ "assets"
|
||||
/ "threads"
|
||||
/ self.HANDLE
|
||||
/ self.EXPECTED_CANONICAL
|
||||
)
|
||||
self.assertTrue(archive_new.is_dir())
|
||||
self.assertTrue(asset_new.is_dir())
|
||||
self.assertTrue((asset_new / "thread_data.json").is_file())
|
||||
self.assertTrue((asset_new / "media.json").is_file())
|
||||
self.assertTrue((archive_new / "index.md").is_file())
|
||||
|
||||
def test_emit_handle_index_links_to_new_path_after_rename(self) -> None:
|
||||
"""The handle index wikilink is updated from the old prefix to
|
||||
the new canonical prefix after rename."""
|
||||
old_dir = "2026-08-24-stale-slug"
|
||||
self._write_existing_pair(
|
||||
dir_name=old_dir,
|
||||
post_id=self.ROOT_ID,
|
||||
)
|
||||
self._write_thread()
|
||||
|
||||
with TemporaryDirectory() as scratch_raw:
|
||||
emit(
|
||||
input_dir=self.input_dir,
|
||||
vault=self.vault,
|
||||
slug=None,
|
||||
archived="2026-08-24",
|
||||
force=True,
|
||||
reconcile_scratch=Path(scratch_raw),
|
||||
frozen_ids=set(),
|
||||
)
|
||||
|
||||
handle_index = self.vault / "archive" / "threads" / self.HANDLE / "index.md"
|
||||
self.assertTrue(handle_index.is_file())
|
||||
text = handle_index.read_text(encoding="utf-8")
|
||||
self.assertIn(
|
||||
f"archive/threads/{self.HANDLE}/{self.EXPECTED_CANONICAL}",
|
||||
text,
|
||||
)
|
||||
self.assertNotIn(f"archive/threads/{self.HANDLE}/{old_dir}", text)
|
||||
|
||||
def test_emit_preserves_draft_false_and_editorial_tags_across_rename(self) -> None:
|
||||
"""Existing ``draft: false`` and a non-mechanical editorial tag
|
||||
survive the rename."""
|
||||
old_dir = "2026-08-24-stale-slug"
|
||||
self._write_existing_pair(
|
||||
dir_name=old_dir,
|
||||
post_id=self.ROOT_ID,
|
||||
editorial_tag="editorial",
|
||||
draft="false",
|
||||
)
|
||||
self._write_thread()
|
||||
|
||||
with TemporaryDirectory() as scratch_raw:
|
||||
emit(
|
||||
input_dir=self.input_dir,
|
||||
vault=self.vault,
|
||||
slug=None,
|
||||
archived="2026-08-24",
|
||||
force=True,
|
||||
reconcile_scratch=Path(scratch_raw),
|
||||
frozen_ids=set(),
|
||||
)
|
||||
|
||||
archive_new = (
|
||||
self.vault
|
||||
/ "archive"
|
||||
/ "threads"
|
||||
/ self.HANDLE
|
||||
/ self.EXPECTED_CANONICAL
|
||||
)
|
||||
text = (archive_new / "index.md").read_text(encoding="utf-8")
|
||||
self.assertIn("draft: false", text)
|
||||
self.assertIn("- editorial", text)
|
||||
|
||||
def test_emit_slug_override_uses_explicit_suffix(self) -> None:
|
||||
"""An explicit ``--slug`` override produces a ``date-manual-name``
|
||||
directory even when a reused old pair exists."""
|
||||
old_dir = "2026-08-24-stale-slug"
|
||||
self._write_existing_pair(
|
||||
dir_name=old_dir,
|
||||
post_id=self.ROOT_ID,
|
||||
)
|
||||
self._write_thread()
|
||||
|
||||
with TemporaryDirectory() as scratch_raw:
|
||||
emit(
|
||||
input_dir=self.input_dir,
|
||||
vault=self.vault,
|
||||
slug="manual-name",
|
||||
archived="2026-08-24",
|
||||
force=True,
|
||||
reconcile_scratch=Path(scratch_raw),
|
||||
frozen_ids=set(),
|
||||
)
|
||||
|
||||
expected = "2026-08-24-manual-name"
|
||||
self.assertTrue(
|
||||
(self.vault / "archive" / "threads" / self.HANDLE / expected).is_dir()
|
||||
)
|
||||
self.assertTrue(
|
||||
(self.vault / "assets" / "threads" / self.HANDLE / expected).is_dir()
|
||||
)
|
||||
self.assertFalse(
|
||||
(self.vault / "archive" / "threads" / self.HANDLE / old_dir).exists()
|
||||
)
|
||||
|
||||
def test_emit_occupied_canonical_target_refuses_before_move(self) -> None:
|
||||
"""A new-thread emit whose desired canonical target is already
|
||||
occupied by another pair must abort with ``SystemExit`` before any
|
||||
write, with no ``-2`` suffix fallback."""
|
||||
canonical_archive, canonical_asset = self._write_existing_pair(
|
||||
dir_name=self.EXPECTED_CANONICAL,
|
||||
post_id="9999",
|
||||
)
|
||||
self._write_thread()
|
||||
|
||||
with TemporaryDirectory() as scratch_raw:
|
||||
with self.assertRaisesRegex(SystemExit, "destination occupied"):
|
||||
emit(
|
||||
input_dir=self.input_dir,
|
||||
vault=self.vault,
|
||||
slug=None,
|
||||
archived="2026-08-24",
|
||||
force=True,
|
||||
reconcile_scratch=Path(scratch_raw),
|
||||
frozen_ids=set(),
|
||||
)
|
||||
|
||||
self.assertFalse(
|
||||
(
|
||||
self.vault
|
||||
/ "archive"
|
||||
/ "threads"
|
||||
/ self.HANDLE
|
||||
/ f"{self.EXPECTED_CANONICAL}-2"
|
||||
).exists()
|
||||
)
|
||||
self.assertFalse(
|
||||
(
|
||||
self.vault
|
||||
/ "assets"
|
||||
/ "threads"
|
||||
/ self.HANDLE
|
||||
/ f"{self.EXPECTED_CANONICAL}-2"
|
||||
).exists()
|
||||
)
|
||||
self.assertTrue(canonical_archive.is_dir())
|
||||
self.assertTrue(canonical_asset.is_dir())
|
||||
|
||||
def test_emit_frozen_mismatch_refuses_before_move(self) -> None:
|
||||
"""A frozen mismatch on the existing asset dir must abort with
|
||||
``SystemExit`` before either directory is moved."""
|
||||
old_dir = "2026-08-24-stale-slug"
|
||||
archive_old, asset_old = self._write_existing_pair(
|
||||
dir_name=old_dir,
|
||||
post_id=self.ROOT_ID,
|
||||
)
|
||||
self._write_thread()
|
||||
|
||||
with TemporaryDirectory() as scratch_raw:
|
||||
with self.assertRaisesRegex(SystemExit, "frozen"):
|
||||
emit(
|
||||
input_dir=self.input_dir,
|
||||
vault=self.vault,
|
||||
slug=None,
|
||||
archived="2026-08-24",
|
||||
force=True,
|
||||
reconcile_scratch=Path(scratch_raw),
|
||||
frozen_ids={self.ROOT_ID},
|
||||
)
|
||||
|
||||
self.assertTrue(archive_old.is_dir())
|
||||
self.assertTrue(asset_old.is_dir())
|
||||
self.assertFalse(
|
||||
(
|
||||
self.vault
|
||||
/ "archive"
|
||||
/ "threads"
|
||||
/ self.HANDLE
|
||||
/ self.EXPECTED_CANONICAL
|
||||
).exists()
|
||||
)
|
||||
self.assertFalse(
|
||||
(
|
||||
self.vault
|
||||
/ "assets"
|
||||
/ "threads"
|
||||
/ self.HANDLE
|
||||
/ self.EXPECTED_CANONICAL
|
||||
).exists()
|
||||
)
|
||||
|
||||
def test_emit_matching_existing_name_emits_normally(self) -> None:
|
||||
"""If the existing reused pair already matches the desired name,
|
||||
emit reuses it without rename and writes the fresh note + media."""
|
||||
archive_canonical, asset_canonical = self._write_existing_pair(
|
||||
dir_name=self.EXPECTED_CANONICAL,
|
||||
post_id=self.ROOT_ID,
|
||||
)
|
||||
self._write_thread()
|
||||
|
||||
with TemporaryDirectory() as scratch_raw:
|
||||
emit(
|
||||
input_dir=self.input_dir,
|
||||
vault=self.vault,
|
||||
slug=None,
|
||||
archived="2026-08-24",
|
||||
force=True,
|
||||
reconcile_scratch=Path(scratch_raw),
|
||||
frozen_ids=set(),
|
||||
)
|
||||
|
||||
self.assertTrue(archive_canonical.is_dir())
|
||||
self.assertTrue(asset_canonical.is_dir())
|
||||
self.assertTrue((archive_canonical / "index.md").is_file())
|
||||
self.assertTrue((asset_canonical / "thread_data.json").is_file())
|
||||
handle_dir = self.vault / "archive" / "threads" / self.HANDLE
|
||||
dirs = sorted(d.name for d in handle_dir.iterdir() if d.is_dir())
|
||||
self.assertEqual(dirs, [self.EXPECTED_CANONICAL])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,494 @@
|
||||
"""Focused tests for the one-time bulk thread-directory reslug plumbing.
|
||||
|
||||
These tests exercise the small planning, pair-rename, recursive-rewrite,
|
||||
apply, and format helpers added to ``emit_archive``. They use compact local
|
||||
fixtures under a ``TemporaryDirectory`` and never touch the real vault.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import ast
|
||||
import json
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from tempfile import TemporaryDirectory
|
||||
from unittest.mock import patch
|
||||
|
||||
from twitter.emit_archive import (
|
||||
ReslugConflict,
|
||||
ReslugMove,
|
||||
ReslugPlan,
|
||||
apply_reslug_plan,
|
||||
format_reslug_plan,
|
||||
plan_reslug,
|
||||
rename_thread_pair,
|
||||
rewrite_archive_paths,
|
||||
)
|
||||
|
||||
|
||||
def _write_pair(
|
||||
vault: Path,
|
||||
*,
|
||||
handle: str = "example",
|
||||
directory: str = "2026-08-24-wrong",
|
||||
title: str = "Canonical title.",
|
||||
date: str = "2026-08-24",
|
||||
post_id: str = "100",
|
||||
) -> tuple[Path, Path]:
|
||||
"""Build a paired archive/asset directory with a thread_data + media json."""
|
||||
archive = vault / "archive" / "threads" / handle / directory
|
||||
assets = vault / "assets" / "threads" / handle / directory
|
||||
archive.mkdir(parents=True, exist_ok=True)
|
||||
assets.mkdir(parents=True, exist_ok=True)
|
||||
(archive / "index.md").write_text(
|
||||
"---\n"
|
||||
f"title: {json.dumps(title)}\n"
|
||||
f"handle: {handle}\n"
|
||||
f'post_id: "{post_id}"\n'
|
||||
f"date: {date}\n"
|
||||
"draft: true\n"
|
||||
"tags:\n"
|
||||
" - archive\n"
|
||||
" - twitter\n"
|
||||
f" - {handle}\n"
|
||||
"---\n\n**1/**\n\nFixture body.\n",
|
||||
encoding="utf-8",
|
||||
newline="\n",
|
||||
)
|
||||
(assets / "thread_data.json").write_text(
|
||||
json.dumps(
|
||||
{
|
||||
"root_post_id": post_id,
|
||||
"source_url": f"https://x.com/{handle}/status/{post_id}",
|
||||
"posts": [
|
||||
{
|
||||
"post_id": post_id,
|
||||
"author": handle,
|
||||
"handle": handle,
|
||||
"text": title,
|
||||
"timestamp": f"{date} 12:00:00",
|
||||
"media_urls": [],
|
||||
"reply_to_id": None,
|
||||
"quote_of_id": None,
|
||||
"metrics": {},
|
||||
}
|
||||
],
|
||||
},
|
||||
indent=2,
|
||||
)
|
||||
+ "\n",
|
||||
encoding="utf-8",
|
||||
newline="\n",
|
||||
)
|
||||
(assets / "media.json").write_text(
|
||||
json.dumps(
|
||||
{
|
||||
"schema_version": 2,
|
||||
"root_post_id": post_id,
|
||||
"items": [],
|
||||
"mirrors": [],
|
||||
},
|
||||
indent=2,
|
||||
)
|
||||
+ "\n",
|
||||
encoding="utf-8",
|
||||
newline="\n",
|
||||
)
|
||||
return archive, assets
|
||||
|
||||
|
||||
class ReslugPlanTests(unittest.TestCase):
|
||||
def test_plan_computes_expected_dir_from_frontmatter(self) -> None:
|
||||
"""Title/date frontmatter must yield the canonical dir name."""
|
||||
with TemporaryDirectory() as raw:
|
||||
vault = Path(raw) / "vault"
|
||||
_write_pair(vault)
|
||||
plan = plan_reslug(vault, set())
|
||||
self.assertFalse(plan.conflicts)
|
||||
self.assertEqual(len(plan.moves), 1)
|
||||
self.assertEqual(plan.moves[0].new_dir_name, "2026-08-24-canonical-title")
|
||||
self.assertEqual(plan.moves[0].old_dir_name, "2026-08-24-wrong")
|
||||
self.assertEqual(plan.moves[0].handle, "example")
|
||||
|
||||
def test_exact_match_is_noop(self) -> None:
|
||||
"""A directory whose name already matches must be reported as a no-op."""
|
||||
with TemporaryDirectory() as raw:
|
||||
vault = Path(raw) / "vault"
|
||||
_write_pair(vault, directory="2026-08-24-canonical-title")
|
||||
plan = plan_reslug(vault, set())
|
||||
self.assertEqual(len(plan.noops), 1)
|
||||
self.assertFalse(plan.moves)
|
||||
self.assertFalse(plan.conflicts)
|
||||
self.assertTrue(plan.can_apply)
|
||||
|
||||
def test_occupied_destination_blocks_entire_plan(self) -> None:
|
||||
"""An occupied archive or asset destination must surface as a conflict."""
|
||||
with TemporaryDirectory() as raw:
|
||||
vault = Path(raw) / "vault"
|
||||
_write_pair(vault)
|
||||
_write_pair(vault, directory="2026-08-24-canonical-title", post_id="200")
|
||||
plan = plan_reslug(vault, set())
|
||||
self.assertTrue(plan.conflicts)
|
||||
self.assertFalse(plan.can_apply)
|
||||
self.assertFalse(plan.moves)
|
||||
|
||||
def test_mismatched_frozen_pair_is_skipped_and_tracked(self) -> None:
|
||||
"""A mismatched frozen pair is skipped and its archive dir is excluded."""
|
||||
with TemporaryDirectory() as raw:
|
||||
vault = Path(raw) / "vault"
|
||||
_write_pair(vault, post_id="300")
|
||||
plan = plan_reslug(vault, {"300"})
|
||||
self.assertEqual(len(plan.frozen), 1)
|
||||
self.assertEqual(len(plan.frozen_dirs), 1)
|
||||
self.assertFalse(plan.moves)
|
||||
self.assertFalse(plan.conflicts)
|
||||
self.assertTrue(plan.can_apply)
|
||||
|
||||
def test_missing_asset_pair_becomes_conflict(self) -> None:
|
||||
"""An archive dir with no asset partner is a blocking conflict."""
|
||||
with TemporaryDirectory() as raw:
|
||||
vault = Path(raw) / "vault"
|
||||
archive = vault / "archive" / "threads" / "example" / "2026-08-24-wrong"
|
||||
archive.mkdir(parents=True)
|
||||
(archive / "index.md").write_text(
|
||||
"---\n"
|
||||
'title: "Canonical title."\n'
|
||||
'handle: example\n'
|
||||
'post_id: "100"\n'
|
||||
"date: 2026-08-24\n"
|
||||
"draft: true\n"
|
||||
"tags:\n"
|
||||
" - archive\n"
|
||||
"---\n\n**1/**\n\nFixture.\n",
|
||||
encoding="utf-8",
|
||||
newline="\n",
|
||||
)
|
||||
plan = plan_reslug(vault, set())
|
||||
self.assertTrue(plan.conflicts)
|
||||
self.assertFalse(plan.moves)
|
||||
|
||||
|
||||
class ReslugRenameTests(unittest.TestCase):
|
||||
def test_rename_moves_both_dirs(self) -> None:
|
||||
"""rename_thread_pair must move assets first and archive second."""
|
||||
with TemporaryDirectory() as raw:
|
||||
vault = Path(raw) / "vault"
|
||||
_write_pair(vault)
|
||||
plan = plan_reslug(vault, set())
|
||||
move = plan.moves[0]
|
||||
rename_thread_pair(move)
|
||||
self.assertFalse(move.archive_old.exists())
|
||||
self.assertFalse(move.asset_old.exists())
|
||||
self.assertTrue(move.archive_new.is_dir())
|
||||
self.assertTrue(move.asset_new.is_dir())
|
||||
|
||||
def test_archive_failure_rolls_asset_back(self) -> None:
|
||||
"""An injected archive rename failure must move the asset back and re-raise."""
|
||||
with TemporaryDirectory() as raw:
|
||||
vault = Path(raw) / "vault"
|
||||
_write_pair(vault)
|
||||
move = plan_reslug(vault, set()).moves[0]
|
||||
original_rename = Path.rename
|
||||
|
||||
def fail_archive(path: Path, destination: Path) -> Path:
|
||||
if path == move.archive_old:
|
||||
raise OSError("injected archive failure")
|
||||
return original_rename(path, destination)
|
||||
|
||||
with patch.object(Path, "rename", fail_archive):
|
||||
with self.assertRaisesRegex(OSError, "injected"):
|
||||
rename_thread_pair(move)
|
||||
self.assertTrue(move.archive_old.is_dir())
|
||||
self.assertTrue(move.asset_old.is_dir())
|
||||
self.assertFalse(move.asset_new.exists())
|
||||
|
||||
|
||||
class ReslugRewriteTests(unittest.TestCase):
|
||||
def test_recursive_rewrite_updates_markdown_canvas_and_branch_paths(self) -> None:
|
||||
"""Markdown and Canvas files inside and outside the thread dir must update."""
|
||||
with TemporaryDirectory() as raw:
|
||||
root = Path(raw)
|
||||
vault = root / "vault"
|
||||
_write_pair(vault)
|
||||
old = "archive/threads/example/2026-08-24-wrong"
|
||||
new = "archive/threads/example/2026-08-24-canonical-title"
|
||||
inbound = vault / "notes" / "inbound.md"
|
||||
canvas = vault / "canvases" / "map.canvas"
|
||||
inbound.parent.mkdir(parents=True)
|
||||
canvas.parent.mkdir(parents=True)
|
||||
inbound.write_text(
|
||||
f"[[{old}/branch]] and [[{old}|alias]]\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
canvas.write_text(
|
||||
json.dumps({"edges": [{"file": f"{old}/branch.md"}]}),
|
||||
encoding="utf-8",
|
||||
)
|
||||
archive_branch = vault / "archive" / "threads" / "example" / "2026-08-24-wrong" / "branch.md"
|
||||
archive_branch.write_text(f"link: [[{old}/index]]\n", encoding="utf-8")
|
||||
changed = rewrite_archive_paths(vault, root / "scratch", {old: new}, ())
|
||||
self.assertIn(new + "/branch", inbound.read_text(encoding="utf-8"))
|
||||
self.assertIn(new + "/branch.md", canvas.read_text(encoding="utf-8"))
|
||||
self.assertIn(new + "/index", archive_branch.read_text(encoding="utf-8"))
|
||||
self.assertIn(canvas, changed)
|
||||
self.assertIn(inbound, changed)
|
||||
self.assertIn(archive_branch, changed)
|
||||
|
||||
def test_replacement_is_one_pass_and_non_cascading(self) -> None:
|
||||
"""Both keys must be applied in one pass; longest key wins at overlap."""
|
||||
with TemporaryDirectory() as raw:
|
||||
vault = Path(raw) / "vault"
|
||||
inbound = vault / "notes" / "inbound.md"
|
||||
inbound.parent.mkdir(parents=True)
|
||||
inbound.write_text(
|
||||
"[[archive/threads/example/old]] and [[archive/threads/example/old-x]]\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
rewrite_archive_paths(
|
||||
vault,
|
||||
Path(raw) / "scratch",
|
||||
{
|
||||
"archive/threads/example/old": "archive/threads/example/new",
|
||||
"archive/threads/example/old-x": "archive/threads/example/new-x",
|
||||
},
|
||||
(),
|
||||
)
|
||||
self.assertEqual(
|
||||
inbound.read_text(encoding="utf-8"),
|
||||
"[[archive/threads/example/new]] and [[archive/threads/example/new-x]]\n",
|
||||
)
|
||||
|
||||
def test_excluded_top_level_dirs_and_frozen_dirs_are_byte_identical(self) -> None:
|
||||
"""site/, assets/, secrets/, .git/, node_modules/, and frozen dirs must not change."""
|
||||
with TemporaryDirectory() as raw:
|
||||
root = Path(raw)
|
||||
vault = root / "vault"
|
||||
_write_pair(vault)
|
||||
old = "archive/threads/example/2026-08-24-wrong"
|
||||
new = "archive/threads/example/2026-08-24-canonical-title"
|
||||
frozen_archive, _ = _write_pair(
|
||||
vault,
|
||||
handle="frozen_author",
|
||||
directory="2024-01-01-locked",
|
||||
title="Locked title.",
|
||||
date="2024-01-01",
|
||||
post_id="999",
|
||||
)
|
||||
frozen_index = frozen_archive / "index.md"
|
||||
frozen_bytes = frozen_index.read_bytes()
|
||||
for excluded in ("site", "assets", "secrets", "node_modules", ".git"):
|
||||
excluded_dir = vault / excluded / "nested"
|
||||
excluded_dir.mkdir(parents=True, exist_ok=True)
|
||||
(excluded_dir / "note.md").write_text(
|
||||
f"see {old} for context\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
rewrite_archive_paths(
|
||||
vault,
|
||||
root / "scratch",
|
||||
{old: new},
|
||||
(frozen_archive,),
|
||||
)
|
||||
self.assertEqual(frozen_index.read_bytes(), frozen_bytes)
|
||||
for excluded in ("site", "assets", "secrets", "node_modules", ".git"):
|
||||
txt = (vault / excluded / "nested" / "note.md").read_text(encoding="utf-8")
|
||||
self.assertIn(old, txt)
|
||||
self.assertNotIn(new, txt)
|
||||
|
||||
|
||||
class ReslugApplyTests(unittest.TestCase):
|
||||
def test_apply_refuses_conflicts_before_any_move(self) -> None:
|
||||
"""apply_reslug_plan must raise before touching disk when conflicts exist."""
|
||||
with TemporaryDirectory() as raw:
|
||||
root = Path(raw)
|
||||
vault = root / "vault"
|
||||
old_archive, old_assets = _write_pair(vault)
|
||||
_write_pair(vault, directory="2026-08-24-canonical-title", post_id="200")
|
||||
plan = plan_reslug(vault, set())
|
||||
with self.assertRaisesRegex(RuntimeError, "conflict"):
|
||||
apply_reslug_plan(plan, root / "scratch")
|
||||
self.assertTrue(old_archive.is_dir())
|
||||
self.assertTrue(old_assets.is_dir())
|
||||
|
||||
def test_apply_moves_pairs_and_rewrites_inbound_links(self) -> None:
|
||||
"""apply_reslug_plan must move both dirs and rewrite inbound links."""
|
||||
with TemporaryDirectory() as raw:
|
||||
root = Path(raw)
|
||||
vault = root / "vault"
|
||||
_write_pair(vault)
|
||||
old = "archive/threads/example/2026-08-24-wrong"
|
||||
new = "archive/threads/example/2026-08-24-canonical-title"
|
||||
inbound = vault / "notes" / "inbound.md"
|
||||
inbound.parent.mkdir(parents=True)
|
||||
inbound.write_text(f"see [[{old}/index]]\n", encoding="utf-8")
|
||||
plan = plan_reslug(vault, set())
|
||||
rewritten = apply_reslug_plan(plan, root / "scratch")
|
||||
self.assertTrue((vault / "archive" / "threads" / "example" / "2026-08-24-canonical-title").is_dir())
|
||||
self.assertTrue((vault / "assets" / "threads" / "example" / "2026-08-24-canonical-title").is_dir())
|
||||
self.assertIn(new, inbound.read_text(encoding="utf-8"))
|
||||
self.assertIn(inbound, rewritten)
|
||||
|
||||
|
||||
class ReslugFormatTests(unittest.TestCase):
|
||||
def test_format_output_is_deterministic_and_complete(self) -> None:
|
||||
"""format_reslug_plan must enumerate noop/rename/conflict lines plus counts deterministically."""
|
||||
with TemporaryDirectory() as raw:
|
||||
vault = Path(raw) / "vault"
|
||||
_write_pair(vault, directory="2026-08-24-canonical-title")
|
||||
_write_pair(
|
||||
vault,
|
||||
handle="other",
|
||||
directory="2024-01-01-old",
|
||||
title="Other title.",
|
||||
date="2024-01-01",
|
||||
post_id="500",
|
||||
)
|
||||
_write_pair(
|
||||
vault,
|
||||
handle="busy",
|
||||
directory="2024-05-05-old",
|
||||
title="Busy title.",
|
||||
date="2024-05-05",
|
||||
post_id="600",
|
||||
)
|
||||
_write_pair(
|
||||
vault,
|
||||
handle="busy",
|
||||
directory="2024-05-05-busy-title",
|
||||
title="Busy title.",
|
||||
date="2024-05-05",
|
||||
post_id="700",
|
||||
)
|
||||
plan = plan_reslug(vault, set())
|
||||
first = format_reslug_plan(plan)
|
||||
second = format_reslug_plan(plan)
|
||||
self.assertEqual(first, second, "format must be deterministic")
|
||||
lines = first.splitlines()
|
||||
noop_lines = [line for line in lines if line.startswith("[noop]")]
|
||||
rename_lines = [line for line in lines if line.startswith("[rename]")]
|
||||
conflict_lines = [line for line in lines if line.startswith("[conflict]")]
|
||||
self.assertGreaterEqual(len(noop_lines), 1)
|
||||
self.assertEqual(len(rename_lines), 1)
|
||||
self.assertEqual(len(conflict_lines), 1)
|
||||
noop_text = "\n".join(noop_lines)
|
||||
self.assertIn(
|
||||
"archive/threads/example/2026-08-24-canonical-title",
|
||||
noop_text,
|
||||
)
|
||||
self.assertIn("archive/threads/other/2024-01-01-old", rename_lines[0])
|
||||
self.assertIn(
|
||||
"archive/threads/other/2024-01-01-other-title",
|
||||
rename_lines[0],
|
||||
)
|
||||
self.assertIn("->", rename_lines[0])
|
||||
self.assertIn("archive/threads/busy", conflict_lines[0])
|
||||
self.assertIn("2024-05-05-old", conflict_lines[0])
|
||||
self.assertTrue(lines[-1].startswith("summary: "))
|
||||
self.assertIn("moves=", lines[-1])
|
||||
self.assertIn("noops=", lines[-1])
|
||||
self.assertIn("conflicts=", lines[-1])
|
||||
self.assertTrue(first.endswith("\n"))
|
||||
|
||||
|
||||
class ReslugModuleOrderTests(unittest.TestCase):
|
||||
def test_reslug_definitions_appear_before_main_guard(self) -> None:
|
||||
"""The reslug block must sit above ``if __name__ == "__main__"``."""
|
||||
module_path = Path(__file__).resolve().parent.parent / "emit_archive.py"
|
||||
tree = ast.parse(module_path.read_text(encoding="utf-8"))
|
||||
names: set[str] = {
|
||||
"ReslugMove",
|
||||
"ReslugConflict",
|
||||
"ReslugPlan",
|
||||
"plan_reslug",
|
||||
"format_reslug_plan",
|
||||
"rename_thread_pair",
|
||||
"rewrite_archive_paths",
|
||||
"apply_reslug_plan",
|
||||
}
|
||||
main_line: int | None = None
|
||||
for node in tree.body:
|
||||
if not isinstance(node, ast.If):
|
||||
continue
|
||||
test = node.test
|
||||
if (
|
||||
isinstance(test, ast.Compare)
|
||||
and len(test.ops) == 1
|
||||
and isinstance(test.ops[0], ast.Eq)
|
||||
and isinstance(test.left, ast.Name)
|
||||
and test.left.id == "__name__"
|
||||
):
|
||||
main_line = node.lineno
|
||||
break
|
||||
self.assertIsNotNone(main_line, "if __name__ == '__main__' block not found")
|
||||
first_def_line: dict[str, int] = {}
|
||||
for node in ast.walk(tree):
|
||||
if isinstance(
|
||||
node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef)
|
||||
) and node.name in names:
|
||||
first_def_line.setdefault(node.name, node.lineno)
|
||||
for name in sorted(names):
|
||||
self.assertIn(name, first_def_line, f"{name} not defined in module")
|
||||
self.assertLess(
|
||||
first_def_line[name],
|
||||
main_line,
|
||||
f"{name} defined at line {first_def_line[name]} "
|
||||
f"is after main guard at line {main_line}",
|
||||
)
|
||||
|
||||
|
||||
class ReslugNoopRewriteTests(unittest.TestCase):
|
||||
def test_nonfrozen_noop_is_not_frozen_and_links_still_rewrite(self) -> None:
|
||||
"""A canonical nonfrozen noop must NOT enter frozen_dirs and its links must still rewrite."""
|
||||
with TemporaryDirectory() as raw:
|
||||
root = Path(raw)
|
||||
vault = root / "vault"
|
||||
# Thread A: nonfrozen mismatched -> rename
|
||||
archive_a, _ = _write_pair(
|
||||
vault,
|
||||
handle="author_a",
|
||||
directory="2024-01-01-old",
|
||||
title="A title.",
|
||||
date="2024-01-01",
|
||||
post_id="111",
|
||||
)
|
||||
# Thread B: canonical nonfrozen noop with a cross-thread link to A's old prefix
|
||||
archive_b, _ = _write_pair(
|
||||
vault,
|
||||
handle="author_b",
|
||||
directory="2024-02-02-b-title",
|
||||
title="B title.",
|
||||
date="2024-02-02",
|
||||
post_id="222",
|
||||
)
|
||||
(archive_b / "index.md").write_text(
|
||||
"---\n"
|
||||
'title: "B title."\n'
|
||||
"handle: author_b\n"
|
||||
'post_id: "222"\n'
|
||||
"date: 2024-02-02\n"
|
||||
"draft: true\n"
|
||||
"tags:\n"
|
||||
" - archive\n"
|
||||
" - twitter\n"
|
||||
" - author_b\n"
|
||||
"---\n\n"
|
||||
"See [[archive/threads/author_a/2024-01-01-old/index]] for context.\n",
|
||||
encoding="utf-8",
|
||||
newline="\n",
|
||||
)
|
||||
plan = plan_reslug(vault, set())
|
||||
self.assertIn(archive_b, plan.noops)
|
||||
self.assertNotIn(archive_b, plan.frozen_dirs)
|
||||
self.assertNotIn(archive_b, plan.frozen)
|
||||
rewritten = apply_reslug_plan(plan, root / "scratch")
|
||||
new_text = (archive_b / "index.md").read_text(encoding="utf-8")
|
||||
self.assertIn(
|
||||
"[[archive/threads/author_a/2024-01-01-a-title/index]]",
|
||||
new_text,
|
||||
)
|
||||
self.assertNotIn("2024-01-01-old", new_text)
|
||||
self.assertIn(archive_b / "index.md", rewritten)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -11,6 +11,7 @@ from tempfile import TemporaryDirectory
|
||||
from unittest.mock import patch
|
||||
|
||||
from twitter import tw as tw_module
|
||||
from twitter.emit_archive import ReslugConflict, ReslugPlan
|
||||
from twitter.models import PostData, PostMetrics, ThreadData, load_thread
|
||||
from twitter.tw import (
|
||||
_capture_ids,
|
||||
@@ -24,6 +25,7 @@ from twitter.tw import (
|
||||
build_parser,
|
||||
cmd_add_branch,
|
||||
cmd_emit,
|
||||
cmd_reslug,
|
||||
)
|
||||
|
||||
|
||||
@@ -458,5 +460,91 @@ class TwMediaCommandTests(unittest.TestCase):
|
||||
self.assertTrue(args.confirm_origin_unavailable)
|
||||
|
||||
|
||||
class ReslugCommandParserTests(unittest.TestCase):
|
||||
def test_reslug_parser_dry_run_defaults(self) -> None:
|
||||
args = build_parser().parse_args(["reslug", "--all"])
|
||||
self.assertEqual(args.cmd, "reslug")
|
||||
self.assertTrue(args.all_threads)
|
||||
self.assertFalse(args.apply)
|
||||
|
||||
def test_reslug_parser_apply_flag(self) -> None:
|
||||
args = build_parser().parse_args(["reslug", "--all", "--apply"])
|
||||
self.assertEqual(args.cmd, "reslug")
|
||||
self.assertTrue(args.all_threads)
|
||||
self.assertTrue(args.apply)
|
||||
|
||||
|
||||
class ReslugCommandDispatchTests(unittest.TestCase):
|
||||
def test_reslug_dry_run_does_not_apply(self) -> None:
|
||||
plan = ReslugPlan(
|
||||
vault=Path("/tmp"),
|
||||
moves=(),
|
||||
noops=(),
|
||||
frozen=(),
|
||||
frozen_dirs=(),
|
||||
conflicts=(),
|
||||
)
|
||||
with (
|
||||
patch.object(tw_module, "plan_reslug", return_value=plan) as plan_fn,
|
||||
patch.object(tw_module, "format_reslug_plan", return_value="") as fmt,
|
||||
patch.object(tw_module, "apply_reslug_plan") as apply,
|
||||
redirect_stdout(StringIO()),
|
||||
):
|
||||
result = cmd_reslug(Namespace(all_threads=True, apply=False))
|
||||
self.assertEqual(result, 0)
|
||||
plan_fn.assert_called_once()
|
||||
fmt.assert_called_once()
|
||||
apply.assert_not_called()
|
||||
|
||||
def test_reslug_conflicts_return_two_without_apply(self) -> None:
|
||||
plan = ReslugPlan(
|
||||
vault=Path("/tmp"),
|
||||
moves=(),
|
||||
noops=(),
|
||||
frozen=(),
|
||||
frozen_dirs=(),
|
||||
conflicts=(ReslugConflict(Path("/tmp/x"), "bad"),),
|
||||
)
|
||||
with (
|
||||
patch.object(tw_module, "plan_reslug", return_value=plan),
|
||||
patch.object(tw_module, "format_reslug_plan", return_value=""),
|
||||
patch.object(tw_module, "apply_reslug_plan") as apply,
|
||||
redirect_stdout(StringIO()),
|
||||
):
|
||||
result = cmd_reslug(Namespace(all_threads=True, apply=False))
|
||||
self.assertEqual(result, 2)
|
||||
apply.assert_not_called()
|
||||
|
||||
def test_reslug_clean_apply_calls_apply_once_with_plan_and_scratch(self) -> None:
|
||||
plan = ReslugPlan(
|
||||
vault=Path("/tmp"),
|
||||
moves=(),
|
||||
noops=(),
|
||||
frozen=(),
|
||||
frozen_dirs=(),
|
||||
conflicts=(),
|
||||
)
|
||||
with (
|
||||
patch.object(tw_module, "plan_reslug", return_value=plan),
|
||||
patch.object(tw_module, "format_reslug_plan", return_value=""),
|
||||
patch.object(tw_module, "apply_reslug_plan", return_value=()) as apply,
|
||||
redirect_stdout(StringIO()),
|
||||
):
|
||||
result = cmd_reslug(Namespace(all_threads=True, apply=True))
|
||||
self.assertEqual(result, 0)
|
||||
apply.assert_called_once()
|
||||
positional = apply.call_args.args
|
||||
self.assertEqual(positional[0], plan)
|
||||
self.assertEqual(positional[1], tw_module.SCRATCH)
|
||||
|
||||
|
||||
class ReslugParserNoCrossContaminationTests(unittest.TestCase):
|
||||
def test_emit_and_refresh_parser_have_no_reslug_attr(self) -> None:
|
||||
emit_args = build_parser().parse_args(["emit", "--id", "100"])
|
||||
refresh_args = build_parser().parse_args(["refresh", "--id", "100"])
|
||||
self.assertFalse(hasattr(emit_args, "reslug"))
|
||||
self.assertFalse(hasattr(refresh_args, "reslug"))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user