完善:同月多文件导入与当前用户离线迁移更新

This commit is contained in:
2026-11-04 11:35:24 +08:00
parent 147b56d0fd
commit f04d4e96e3
89 changed files with 6634 additions and 201 deletions
+148
View File
@@ -0,0 +1,148 @@
import gzip
import hashlib
import io
import json
import os
from pathlib import Path
import sqlite3
import sys
import tarfile
import tempfile
import unittest
from unittest.mock import patch
HERE = Path(__file__).resolve().parent
sys.path.insert(0, str(HERE))
import control
import restore_migration as restore
from migration_common import dump_counts, extract_checked, filter_models, inserted_rows, path_under_source, relative_name
from prepare_migration import native_copy
class MigrationValidationTest(unittest.TestCase):
def test_extended_insert_counts_ignore_strings_and_escaped_quotes(self):
cases = [
(b"INSERT INTO `x` VALUES (1,'a'),(2,'b');\n", 2),
(b"INSERT INTO `x` VALUES (1,'fake ),( delimiter'),(2,'quote \\' ),( escaped'),(3,0x001122);\n", 3),
(b"INSERT INTO `x` VALUES ('a\\\\',NULL,'(nested)'),(NULL,'\\n',4);\n", 2),
(b"INSERT INTO `x` VALUES ('\xe4\xb8\xad\xe6\x96\x87', '{\\\"text\\\":\\\"),(\\\"}');\n", 1),
]
for statement, count in cases:
with self.subTest(statement=statement):
self.assertEqual(inserted_rows(statement), count)
def test_dump_counts_include_empty_tables(self):
with tempfile.TemporaryDirectory() as folder:
path = Path(folder) / 'dump.gz'
with gzip.open(path, 'wb') as stream:
stream.write(b"CREATE TABLE `empty` (id INT);\nCREATE TABLE `x` (id INT);\nINSERT INTO `x` VALUES (1),(2);\n")
counts, size = dump_counts(path)
self.assertEqual(counts, {'empty': 0, 'x': 2})
self.assertGreater(size, 0)
def test_mimo_profiles_and_root_selection_are_excluded(self):
config = {'revision': 'original', 'activeProfileId': 'mimo', 'profiles': [
{'id': 'mimo', 'name': 'MiMo', 'model': 'mimo-v2.6', 'apiKey': 'exclude-this', 'baseUrl': 'https://api.example.test'},
{'id': 'other', 'name': 'Qwen', 'model': 'qwen-test', 'apiKey': 'retain-this', 'baseUrl': 'https://other.example.test'}]}
filtered, removed = filter_models(config)
self.assertEqual(len(removed), 1)
self.assertEqual(filtered['activeProfileId'], 'other')
self.assertEqual(filtered['apiKey'], 'retain-this')
self.assertEqual(filtered['revision'], 'original')
self.assertNotIn('exclude-this', json.dumps(filtered))
self.assertEqual(len(filtered['profiles']), 1)
def test_mac_path_is_parsed_portably(self):
self.assertEqual(path_under_source('/Users/a/archive/source/文件.xlsx', '/Users/a/archive'), 'source/文件.xlsx')
for name in ('../outside', '/absolute', 'C:/data', 'a\\b', '.', 'a/./b', 'CON.txt', 'x/name.'):
with self.subTest(name=name), self.assertRaises(ValueError):
relative_name(name)
with self.assertRaises(ValueError):
path_under_source('/Users/a/archive-other/secret', '/Users/a/archive')
def test_archive_rejects_traversal_links_duplicates_and_bad_hash(self):
for kind in ('traversal', 'link', 'duplicate', 'checksum'):
with self.subTest(kind=kind), tempfile.TemporaryDirectory() as directory:
folder = Path(directory)
archive = folder / 'archive.gz'
body = b'contents'
with tarfile.open(archive, 'w:gz') as output:
info = tarfile.TarInfo('../outside' if kind == 'traversal' else 'file')
if kind == 'link':
info.type, info.linkname = tarfile.SYMTYPE, '/outside'
output.addfile(info)
else:
info.size = len(body)
output.addfile(info, io.BytesIO(body))
if kind == 'duplicate':
output.addfile(info, io.BytesIO(body))
expected = {'file': {'bytes': len(body), 'sha256': '0' * 64 if kind == 'checksum' else hashlib.sha256(body).hexdigest()}}
with self.assertRaises(ValueError):
extract_checked(archive, folder / 'target', expected)
self.assertFalse((folder / 'outside').exists())
def test_native_history_preserves_chats_and_scrubs_credentials(self):
with tempfile.TemporaryDirectory() as directory:
folder = Path(directory)
source = folder / 'source.sqlite'
db = sqlite3.connect(source)
db.executescript("CREATE TABLE chat(id TEXT,body TEXT);INSERT INTO chat VALUES('a','history');"
"CREATE TABLE project_llm_config(api_key TEXT);INSERT INTO project_llm_config VALUES('mimo-exclusive-secret');"
"CREATE TABLE session(token TEXT);INSERT INTO session VALUES('transient-token');")
db.commit()
db.close()
target = folder / 'target.sqlite'
native_copy(source, target)
migrated = sqlite3.connect(target)
self.assertEqual(migrated.execute('SELECT body FROM chat').fetchone()[0], 'history')
self.assertEqual(migrated.execute('SELECT COUNT(*) FROM project_llm_config').fetchone()[0], 0)
self.assertEqual(migrated.execute('SELECT COUNT(*) FROM session').fetchone()[0], 0)
migrated.close()
self.assertNotIn(b'mimo-exclusive-secret', target.read_bytes())
class MigrationCommitTest(unittest.TestCase):
def test_partial_file_switch_rolls_back_existing_files_and_configuration(self):
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
private = root / '.runtime'
private.mkdir()
target = private / 'nao-users'
target.mkdir()
(target / 'old').write_text('original')
staged = private / 'stage-native'
staged.mkdir()
(staged / 'new').write_text('new')
model = private / 'nao-model-config.json'
model.write_text('original model')
attempt = 'a' * 16
plan = {'attempt': attempt, 'previousConfig': {'previous': True}, 'newConfig': {'new': True}, 'privateSwitches': [
{'target': '.runtime/nao-users', 'previous': '.runtime/previous-native', 'staged': '.runtime/stage-native', 'failed': '.runtime/failed-native', 'existed': True},
{'target': '.runtime/nao-model-config.json', 'previous': '.runtime/previous-model', 'staged': '.runtime/missing-stage-model', 'failed': '.runtime/failed-model', 'existed': True}]}
with patch.multiple(restore, ROOT=root, JOURNAL=private / 'journal', RECEIPT=private / 'receipt'), patch.object(control, 'CONFIG', private / 'deployment.json'):
with self.assertRaises(FileNotFoundError):
restore.commit_plan(plan, {})
self.assertEqual((target / 'old').read_text(), 'original')
self.assertEqual(model.read_text(), 'original model')
self.assertEqual(json.loads((private / 'deployment.json').read_text()), {'previous': True})
self.assertTrue((private / 'failed-native/new').exists())
def test_repeated_restore_does_not_reimport_old_snapshot(self):
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
payload = root / 'migration'
payload.mkdir()
migration_id = 'a' * 32
(payload / 'manifest.json').write_text(json.dumps({'formatVersion': 1, 'migrationId': migration_id}))
receipt = root / 'receipt'
value = {'migrationId': migration_id, 'database': 'quality_restore_' + 'b' * 32}
receipt.write_text(json.dumps(value))
config = {'restoredFromMigration': migration_id, 'mysqlDatabase': value['database']}
with patch.multiple(restore, PAYLOAD=payload, RECEIPT=receipt, JOURNAL=root / 'journal'), patch.object(control, 'configuration', return_value=config), patch.object(restore, 'stop_owned_service') as stop, patch.object(restore, 'run_query') as query:
self.assertEqual(restore.restore_locked(False), value)
stop.assert_not_called()
query.assert_not_called()
if __name__ == '__main__':
unittest.main()