import { McpServerForm } from '$lib/components/app/mcp';
import { mcpStore } from '$lib/stores/mcp.svelte';
import { settingsStore } from '$lib/stores/settings.svelte';
import { beforeEach, describe, expect, it } from 'vitest';
import { render } from 'vitest-browser-svelte';

describe('mcp server display name', () => {
	beforeEach(() => {
		settingsStore.updateConfig('mcpServers', '[]');
	});

	it('custom display name wins over the url fallback', () => {
		const server = mcpStore.addServer({
			displayName: 'My Tools',
			enabled: false,
			url: 'https://mcp.example.com/a'
		});

		expect(mcpStore.getServerLabel(server)).toBe('My Tools');
	});

	it('without a custom name the url is the label', () => {
		const server = mcpStore.addServer({ enabled: false, url: 'https://mcp.example.com/a' });

		expect(mcpStore.getServerLabel(server)).toBe('https://mcp.example.com/a');
	});

	it('identical labels get positional suffixes', () => {
		const a = mcpStore.addServer({
			displayName: 'GitHub',
			enabled: false,
			url: 'https://mcp.example.com/a'
		});
		const b = mcpStore.addServer({
			displayName: 'GitHub',
			enabled: false,
			url: 'https://mcp.example.com/b'
		});

		expect(mcpStore.getServerLabel(a)).toBe('GitHub (1)');
		expect(mcpStore.getServerLabel(b)).toBe('GitHub (2)');
	});

	it('renaming one twin dissolves the suffixes', () => {
		const a = mcpStore.addServer({
			displayName: 'GitHub',
			enabled: false,
			url: 'https://mcp.example.com/a'
		});
		const b = mcpStore.addServer({
			displayName: 'GitHub',
			enabled: false,
			url: 'https://mcp.example.com/b'
		});

		mcpStore.updateServer(b.id, { displayName: 'GitHub Work' });
		expect(mcpStore.getServerLabel(a)).toBe('GitHub');
		expect(mcpStore.getServerLabel(mcpStore.getServerById(b.id)!)).toBe('GitHub Work');
	});

	it('the form exposes an editable display name field', async () => {
		let captured = '';

		const screen = await render(McpServerForm, {
			headers: '',
			name: '',
			onHeadersChange: () => {},
			onNameChange: (v: string) => (captured = v),
			onUrlChange: () => {},
			url: 'https://mcp.example.com/a'
		});
		const input = screen.getByLabelText('Display name');

		await expect.element(input).toBeVisible();
		await input.fill('My Custom Server');
		expect(captured).toBe('My Custom Server');
	});
});
