import json
import sys
import tempfile
import unittest
import zipfile
from pathlib import Path

SCRIPTS_DIR = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(SCRIPTS_DIR))

import sanitize_backtest_archive as mod  # noqa: E402


class SanitizeArchiveTest(unittest.TestCase):
    def test_removes_only_embedded_config(self):
        with tempfile.TemporaryDirectory() as directory:
            root = Path(directory)
            source = root / "raw.zip"
            destination = root / "safe.zip"
            with zipfile.ZipFile(source, "w") as archive:
                archive.writestr("result.json", json.dumps({"strategy": {}}))
                archive.writestr("result_config.json", json.dumps({"api_key": "secret"}))
                archive.writestr("result_wallet.feather", b"wallet")

            result = mod.sanitize_archive(source, destination)

            self.assertEqual(result["removed_members"], ["result_config.json"])
            with zipfile.ZipFile(destination) as archive:
                self.assertEqual(
                    sorted(archive.namelist()),
                    ["result.json", "result_wallet.feather"],
                )

    def test_requires_distinct_paths(self):
        with tempfile.TemporaryDirectory() as directory:
            path = Path(directory) / "result.zip"
            with zipfile.ZipFile(path, "w") as archive:
                archive.writestr("result_config.json", "{}")
            with self.assertRaises(ValueError):
                mod.sanitize_archive(path, path)


if __name__ == "__main__":
    unittest.main()
