BareGit
import unittest

from diffusion_cli.errors import DiffusionCliError
from diffusion_cli.server import ServerConfig, createApp, validateServerConfig


class FakeGenerationService:
    pass


class ServerTest(unittest.TestCase):
    def testValidateServerConfigAcceptsKnownProfile(self):
        config = validateServerConfig(
            "sillytavern-sdcpp",
            "127.0.0.1",
            7860,
            "cpu-cache",
        )

        self.assertEqual(config.api_profile, "sillytavern-sdcpp")
        self.assertEqual(config.model_residency, "cpu-cache")

    def testValidateServerConfigRejectsUnknownProfile(self):
        with self.assertRaises(DiffusionCliError) as context:
            validateServerConfig("native-sdcpp", "127.0.0.1", 7860, "staged")

        self.assertIn("Unknown API profile", str(context.exception))

    def testValidateServerConfigRejectsInvalidPort(self):
        with self.assertRaises(DiffusionCliError) as context:
            validateServerConfig("sillytavern-sdcpp", "127.0.0.1", 0, "staged")

        self.assertIn("--port must be", str(context.exception))

    def testValidateServerConfigRejectsInvalidModelResidency(self):
        with self.assertRaises(DiffusionCliError) as context:
            validateServerConfig("sillytavern-sdcpp", "127.0.0.1", 7860, "vram")

        self.assertIn("--model-residency", str(context.exception))

    def testCreateAppRegistersHealthForSelectedProfile(self):
        app = createApp(
            ServerConfig("sillytavern-sdcpp", "127.0.0.1", 7860, "staged"),
            FakeGenerationService(),
        )

        response = app.test_client().get("/health")

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.json["api_profile"], "sillytavern-sdcpp")


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