'use strict';

// src/testing/mock-agent-service.ts
var MockAgent = class {
  sessionId = void 0;
  _state = "idle";
  _config;
  _onRun;
  _onStream;
  constructor(config, options) {
    this._config = config;
    this._onRun = options?.onRun;
    this._onStream = options?.onStream;
  }
  async run(prompt, options) {
    if (this._onRun) return this._onRun(prompt, options);
    return { output: "Mock response", structuredOutput: void 0, toolCalls: [], messages: [], usage: { promptTokens: 10, completionTokens: 5 } };
  }
  async runWithContext(messages, options) {
    const lastMsg = messages[messages.length - 1];
    const text = typeof lastMsg?.content === "string" ? lastMsg.content : "context";
    return this.run(text, options);
  }
  async runStructured(prompt, _schema, options) {
    const base = await this.run(prompt, options);
    return { ...base, structuredOutput: void 0 };
  }
  async *stream(prompt, options) {
    if (this._onStream) {
      yield* this._onStream(prompt, options);
      return;
    }
    yield { type: "text_delta", text: "Mock " };
    yield { type: "text_delta", text: "response" };
    yield { type: "done", finalOutput: "Mock response" };
  }
  async *streamWithContext(messages, options) {
    const lastMsg = messages[messages.length - 1];
    const text = typeof lastMsg?.content === "string" ? lastMsg.content : "context";
    yield* this.stream(text, options);
  }
  abort() {
    this._state = "idle";
  }
  async interrupt() {
    this._state = "idle";
  }
  getState() {
    return this._state;
  }
  getConfig() {
    return this._config;
  }
  dispose() {
    this._state = "disposed";
  }
};
function createMockAgentService(options = {}) {
  const name = options.name ?? "mock";
  return {
    name,
    createAgent(config) {
      return new MockAgent(config, options);
    },
    async listModels() {
      return options.models ?? [
        { id: "mock-model-1", name: "Mock Model 1" },
        { id: "mock-model-2", name: "Mock Model 2" }
      ];
    },
    async validate() {
      return options.validationResult ?? { valid: true, errors: [] };
    },
    async dispose() {
    }
  };
}

// src/chat/types.ts
function createChatId() {
  return crypto.randomUUID();
}
var UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
function toChatId(value) {
  if (!UUID_RE.test(value)) {
    throw new TypeError(`Invalid ChatId: "${value}" is not a valid UUID`);
  }
  return value;
}

// src/testing/mock-data.ts
function createMockSession(options = {}) {
  const id = options.id ?? createChatId();
  const now = (/* @__PURE__ */ new Date()).toISOString();
  return {
    id,
    title: options.title ?? "Test Session",
    messages: options.messages ?? [],
    config: {
      model: options.config?.model ?? "test-model",
      backend: options.config?.backend ?? "test-backend",
      systemPrompt: options.config?.systemPrompt ?? ""
    },
    metadata: {
      messageCount: options.messages?.length ?? 0,
      totalTokens: 0,
      custom: options.metadata ?? {}
    },
    status: options.status ?? "active",
    createdAt: now,
    updatedAt: now
  };
}
function createMockMessage(options = {}) {
  const id = createChatId();
  const parts = options.parts ?? (options.text ? [{ type: "text", text: options.text, status: "complete" }] : [{ type: "text", text: "Test message", status: "complete" }]);
  const now = (/* @__PURE__ */ new Date()).toISOString();
  return {
    id,
    role: options.role ?? "user",
    parts,
    status: options.status ?? "complete",
    createdAt: now,
    metadata: options.metadata ?? {}
  };
}

// src/testing/mock-runtime.ts
function createMockRuntime(options = {}) {
  const sessions = /* @__PURE__ */ new Map();
  const tools = /* @__PURE__ */ new Map();
  const sessionListeners = /* @__PURE__ */ new Set();
  let currentBackend = options.defaultBackend ?? "mock";
  let status = "idle";
  for (const s of options.sessions ?? []) {
    sessions.set(s.id, s);
  }
  function notifySessionChange() {
    for (const cb of sessionListeners) {
      try {
        cb();
      } catch {
      }
    }
  }
  const runtime = {
    get status() {
      return status;
    },
    get registeredTools() {
      return tools;
    },
    async createSession(opts) {
      const session = createMockSession({
        title: opts.title,
        config: {
          model: opts.config?.model ?? "mock-model",
          backend: opts.config?.backend ?? currentBackend,
          systemPrompt: opts.config?.systemPrompt
        },
        metadata: opts.custom
      });
      sessions.set(session.id, session);
      notifySessionChange();
      return session;
    },
    async getSession(id) {
      return sessions.get(toChatId(String(id))) ?? null;
    },
    async listSessions(_opts) {
      return [...sessions.values()];
    },
    async deleteSession(id) {
      sessions.delete(toChatId(String(id)));
      notifySessionChange();
    },
    send(sessionId, message, sendOpts) {
      if (options.onSend) return options.onSend(sessionId, message, sendOpts);
      async function* defaultStream() {
        const msgId = createChatId();
        yield { type: "message:start", messageId: msgId };
        yield { type: "message:delta", text: "Mock reply" };
        yield { type: "message:complete", messageId: msgId };
        yield { type: "done", finalOutput: "Mock reply" };
        const session = sessions.get(toChatId(String(sessionId)));
        if (session) {
          session.messages.push(createMockMessage({ role: "assistant", text: "Mock reply" }));
          session.metadata.messageCount = session.messages.length;
        }
        notifySessionChange();
      }
      return defaultStream();
    },
    abort() {
    },
    async listModels() {
      return options.models ?? [{ id: "mock-model", name: "Mock Model" }];
    },
    async listBackends() {
      return [{ name: currentBackend }];
    },
    onSessionChange(callback) {
      sessionListeners.add(callback);
      return () => {
        sessionListeners.delete(callback);
      };
    },
    registerTool(tool) {
      tools.set(tool.name, tool);
    },
    removeTool(name) {
      tools.delete(name);
    },
    use(mw) {
    },
    removeMiddleware(mw) {
    },
    async getContextStats(_sessionId) {
      return null;
    },
    async dispose() {
      status = "disposed";
      sessions.clear();
      tools.clear();
      sessionListeners.clear();
    }
  };
  return runtime;
}

// src/testing/mock-chat-client.ts
function createMockChatClient(options = {}) {
  const sessions = /* @__PURE__ */ new Map();
  const providers = /* @__PURE__ */ new Map();
  const sessionListeners = /* @__PURE__ */ new Set();
  const selectionListeners = /* @__PURE__ */ new Set();
  let activeSessionId = null;
  let selectedProviderId = null;
  let status = "idle";
  for (const s of options.sessions ?? []) {
    sessions.set(s.id, s);
  }
  for (const p of options.providers ?? []) {
    providers.set(p.id, p);
  }
  function notifySessionChange() {
    for (const cb of sessionListeners) {
      try {
        cb();
      } catch {
      }
    }
  }
  const client = {
    get status() {
      return status;
    },
    get activeSessionId() {
      return activeSessionId;
    },
    async createSession(opts) {
      const session = createMockSession({
        title: opts.title,
        config: {
          model: opts.config?.model ?? "mock-model",
          backend: opts.config?.backend ?? "mock",
          systemPrompt: opts.config?.systemPrompt
        }
      });
      sessions.set(session.id, session);
      activeSessionId = session.id;
      notifySessionChange();
      return session;
    },
    async getSession(id) {
      return sessions.get(toChatId(String(id))) ?? null;
    },
    async listSessions(_opts) {
      return [...sessions.values()];
    },
    async deleteSession(id) {
      sessions.delete(toChatId(String(id)));
      if (activeSessionId === String(id)) activeSessionId = null;
      notifySessionChange();
    },
    async switchSession(id) {
      const session = sessions.get(toChatId(String(id)));
      if (!session) throw new Error(`Session ${id} not found`);
      activeSessionId = session.id;
      return session;
    },
    send(sessionId, message, sendOpts) {
      if (options.onSend) return options.onSend(sessionId, message, sendOpts);
      async function* defaultStream() {
        const msgId = createChatId();
        yield { type: "message:start", messageId: msgId };
        yield { type: "message:delta", text: "Mock reply" };
        yield { type: "message:complete", messageId: msgId };
        yield { type: "done", finalOutput: "Mock reply" };
        const session = sessions.get(toChatId(String(sessionId)));
        if (session) {
          session.messages.push(createMockMessage({ role: "assistant", text: "Mock reply" }));
          session.metadata.messageCount = session.messages.length;
        }
        notifySessionChange();
      }
      return defaultStream();
    },
    abort() {
    },
    // ── Provider Selection ──
    get selectedProviderId() {
      return selectedProviderId;
    },
    selectProvider(providerId) {
      selectedProviderId = providerId;
      for (const cb of selectionListeners) {
        try {
          cb(providerId);
        } catch {
        }
      }
    },
    onSelectionChange(callback) {
      selectionListeners.add(callback);
      return () => {
        selectionListeners.delete(callback);
      };
    },
    async listModels() {
      return options.models ?? [{ id: "mock-model", name: "Mock Model" }];
    },
    async listBackends() {
      return [{ name: "mock" }];
    },
    onSessionChange(callback) {
      sessionListeners.add(callback);
      return () => {
        sessionListeners.delete(callback);
      };
    },
    async getContextStats(_sessionId) {
      return null;
    },
    // ── Provider CRUD ──
    async listProviders() {
      return [...providers.values()];
    },
    async createProvider(config) {
      const provider = {
        ...config,
        id: createChatId(),
        createdAt: Date.now()
      };
      providers.set(provider.id, provider);
      return provider;
    },
    async updateProvider(id, changes) {
      const existing = providers.get(id);
      if (!existing) throw new Error(`Provider ${id} not found`);
      providers.set(id, { ...existing, ...changes });
    },
    async deleteProvider(id) {
      providers.delete(id);
    },
    async dispose() {
      status = "disposed";
      sessions.clear();
      providers.clear();
      sessionListeners.clear();
      selectionListeners.clear();
    }
  };
  return client;
}

exports.createMockAgentService = createMockAgentService;
exports.createMockChatClient = createMockChatClient;
exports.createMockMessage = createMockMessage;
exports.createMockRuntime = createMockRuntime;
exports.createMockSession = createMockSession;
//# sourceMappingURL=testing.cjs.map
//# sourceMappingURL=testing.cjs.map