2026-07-24 21:11:02 +08:00
from __future__ import annotations
import json
import sqlite3
import sys
import tempfile
import unittest
from pathlib import Path
ROOT = Path ( __file__ ) . resolve ( ) . parents [ 1 ]
sys . path . insert ( 0 , str ( ROOT ) )
test: locate the companion repo instead of assuming one layout
test_m6_r2_real_dto_shape resolved the companion repo as a sibling directory,
which is only true of the development checkout. On the VM the skipif was
always true, so the seven cases that exercise the companion's real DTO builder
-- the only genuine check of the cross-plugin envelope shape -- never ran in
the environment that most needed them, and a skip shows up as a missing dot
rather than a failure.
test_ops_migration had the harder version of the same bug: the import simply
failed, and working around it with PYTHONPATH let the memory side's own
core/ops_migration shadow the companion module, producing two failures that
looked like product defects and were not.
Resolution now lives in tests/_companion_repo.py and tries, in order, the
COMPANION_PLUGIN_ROOT override, the sibling development directory, the
sibling deployed directory (_ops suffix), and one level up. The path helper
inserts at the front of sys.path deliberately: getting that order wrong fails
as "green but testing the wrong module", which is worse than an ImportError.
Memory suite 330 passed.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-27 05:41:32 +08:00
sys . path . insert ( 0 , str ( Path ( __file__ ) . resolve ( ) . parent ) )
from _companion_repo import ensure_companion_on_path # noqa: E402
# 必须排在记忆侧之前:记忆侧也有 core/ops_migration.py,顺序反了会 import 到同名的
# 那一个,测试会以「跑绿了但测错了对象」的形态失败,比 ImportError 更难查。
_COMPANION_ROOT = ensure_companion_on_path ( )
if _COMPANION_ROOT is None :
import pytest
pytest . skip ( " 找不到陪伴插件仓库,跨仓迁移测试跳过(可用 COMPANION_PLUGIN_ROOT 指定) " ,
allow_module_level = True )
2026-07-24 21:11:02 +08:00
import ops_migration as companion_migration
from core import ops_migration as memory_migration
def make_companion_db ( path : Path ) - > None :
connection = sqlite3 . connect ( path )
connection . execute (
" CREATE TABLE store_sections (section_name TEXT PRIMARY KEY, payload_json TEXT NOT NULL, updated_at REAL NOT NULL, checksum TEXT, schema_version INTEGER) "
)
connection . execute (
" INSERT INTO store_sections VALUES ( ' daily_plan ' , ' { \" today \" :true} ' , 1, ' ' , 1) "
)
connection . commit ( )
connection . close ( )
def make_memory_db ( path : Path ) - > None :
connection = sqlite3 . connect ( path )
for table in ( " memories " , " identities " , " timeline " ) :
connection . execute ( f " CREATE TABLE { table } (id TEXT PRIMARY KEY) " )
connection . execute ( " INSERT INTO memories VALUES ( ' memory-1 ' ) " )
connection . commit ( )
connection . close ( )
class OpsMigrationTests ( unittest . TestCase ) :
def test_companion_json_preview_and_execute_preserves_fields ( self ) - > None :
with tempfile . TemporaryDirectory ( ) as temp :
root = Path ( temp )
source = root / " old "
target = root / " new "
source . mkdir ( )
payload = {
" users " : { " u1 " : { " preference " : " tea " } } ,
" daily_plan " : { " date " : " 2026-07-24 " } ,
" important_dates " : [ { " name " : " birthday " } ] ,
" bookshelf_items " : [ { " title " : " private " } ] ,
" creative_projects " : [ { " title " : " novel " } ] ,
" news_integration " : { " enabled " : True } ,
}
( source / " companions.json " ) . write_text ( json . dumps ( payload , ensure_ascii = False ) , encoding = " utf-8 " )
report = companion_migration . preview ( source , target )
self . assertTrue ( report [ " ok " ] )
self . assertIn ( " json:daily_plan " , report [ " retained_fields " ] )
self . assertIn ( " news_integration " , report [ " flagged_fields " ] )
result = companion_migration . execute ( source , target )
self . assertTrue ( result [ " ok " ] )
self . assertEqual ( payload , json . loads ( ( target / " companions.json " ) . read_text ( encoding = " utf-8 " ) ) )
self . assertTrue ( ( target / companion_migration . MARKER_NAME ) . is_file ( ) )
self . assertFalse ( companion_migration . execute ( source , target ) [ " ok " ] )
def test_companion_sqlite_preview_and_execute_keeps_json_mirror ( self ) - > None :
with tempfile . TemporaryDirectory ( ) as temp :
root = Path ( temp )
source = root / " old "
target = root / " new "
source . mkdir ( )
make_companion_db ( source / " companions.db " )
( source / " companions.json " ) . write_text ( ' { " creative_projects " : []} ' , encoding = " utf-8 " )
result = companion_migration . execute ( source , target )
self . assertTrue ( result [ " ok " ] )
self . assertTrue ( ( target / " companions.db " ) . is_file ( ) )
self . assertTrue ( ( target / " companions.json " ) . is_file ( ) )
check = sqlite3 . connect ( target / " companions.db " )
self . assertEqual ( 1 , check . execute ( " SELECT COUNT(*) FROM store_sections " ) . fetchone ( ) [ 0 ] )
check . close ( )
def test_memory_migration_preserves_database_and_state ( self ) - > None :
with tempfile . TemporaryDirectory ( ) as temp :
root = Path ( temp )
source = root / " old "
target = root / " new "
source . mkdir ( )
make_memory_db ( source / memory_migration . DATABASE_NAME )
( source / " memory_companion_sleep_state.json " ) . write_text ( ' { " sleeping " :false} ' , encoding = " utf-8 " )
report = memory_migration . preview ( source , target )
self . assertTrue ( report [ " ok " ] )
self . assertIn ( " memories " , report [ " retained_tables " ] )
result = memory_migration . execute ( source , target )
self . assertTrue ( result [ " ok " ] )
check = sqlite3 . connect ( target / memory_migration . DATABASE_NAME )
self . assertEqual ( 1 , check . execute ( " SELECT COUNT(*) FROM memories " ) . fetchone ( ) [ 0 ] )
check . close ( )
self . assertEqual ( ' { " sleeping " :false} ' , ( target / " memory_companion_sleep_state.json " ) . read_text ( encoding = " utf-8 " ) )
def test_non_empty_target_and_invalid_source_are_rejected_without_changes ( self ) - > None :
with tempfile . TemporaryDirectory ( ) as temp :
root = Path ( temp )
source = root / " old "
target = root / " new "
source . mkdir ( )
target . mkdir ( )
( target / " keep.txt " ) . write_text ( " keep " , encoding = " utf-8 " )
( source / " companions.json " ) . write_text ( " [] " , encoding = " utf-8 " )
report = companion_migration . preview ( source , target )
self . assertFalse ( report [ " ok " ] )
self . assertIn ( " 非空 " , report [ " errors " ] [ 0 ] )
self . assertEqual ( " keep " , ( target / " keep.txt " ) . read_text ( encoding = " utf-8 " ) )
def test_memory_invalid_database_is_rejected ( self ) - > None :
with tempfile . TemporaryDirectory ( ) as temp :
root = Path ( temp )
source = root / " old "
source . mkdir ( )
( source / memory_migration . DATABASE_NAME ) . write_bytes ( b " not sqlite " )
report = memory_migration . preview ( source , root / " new " )
self . assertFalse ( report [ " ok " ] )
self . assertTrue ( report [ " errors " ] )
if __name__ == " __main__ " :
unittest . main ( )