/**
 * SDK Test Runner — Domain: knowledge
 */
(function () {
  'use strict';
  var T = window.SDKR;
  if (!T) return;

  T.registerDomain('knowledge', async function () {
    var tag = 'sdkr-' + Date.now();
    var kbId = null;

    await T.test('knowledge', 'list', 'sw.api.knowledge.list()', {
      sdk:      function () { return sw.api.knowledge.list(); },
      raw:      { method: 'GET', path: '/knowledge-bases' },
      validate: function (r) { var arr = T.unwrapList(r); T.assert(arr.length >= 0, 'expected list'); }
    });

    await T.test('knowledge', 'crud', 'sw.api.knowledge.create()', {
      sdk: function () {
        return sw.api.knowledge.create({ name: tag + '-kb', scope: 'personal' });
      },
      raw: { method: 'POST', path: '/knowledge-bases', body: { name: tag + '-kb', scope: 'personal' } },
      validate: function (r) {
        T.assertShape(r, T.S.kb, 'kb');
        kbId = r.id;
        T.registerCleanup(function () { if (kbId) return T.safeDelete('/knowledge-bases/' + kbId); });
      }
    });

    if (!kbId) return;

    await T.test('knowledge', 'crud', 'sw.api.knowledge.get(id)', {
      sdk:      function () { return sw.api.knowledge.get(kbId); },
      raw:      { method: 'GET', path: '/knowledge-bases/' + kbId },
      validate: function (r) {
        T.assertShape(r, T.S.kb, 'kb');
        T.assert(r.id === kbId, 'id mismatch');
      }
    });

    await T.test('knowledge', 'crud', 'sw.api.knowledge.update(id, data)', {
      sdk:      function () { return sw.api.knowledge.update(kbId, { name: tag + '-kb-upd' }); },
      raw:      { method: 'PUT', path: '/knowledge-bases/' + kbId, body: { name: tag + '-kb-upd' } },
      validate: function (r) { T.assertShape(r, T.S.kb, 'kb'); }
    });

    // ── Discoverable ──

    await T.test('knowledge', 'discover', 'sw.api.knowledge.discoverable()', {
      sdk:      function () { return sw.api.knowledge.discoverable(); },
      raw:      { method: 'GET', path: '/knowledge-bases-discoverable' },
      validate: function (r) { var arr = T.unwrapList(r); T.assert(arr.length >= 0, 'expected list'); }
    });

    // ── Documents ──

    await T.test('knowledge', 'docs', 'sw.api.knowledge.documents(id)', {
      sdk:      function () { return sw.api.knowledge.documents(kbId); },
      raw:      { method: 'GET', path: '/knowledge-bases/' + kbId + '/documents' },
      validate: function (r) { var arr = T.unwrapList(r); T.assert(arr.length >= 0, 'expected list'); }
    });

    // ── Search ──

    // Search requires an embedding provider — only run if fixture provisioned one
    var hasEmbedding = T.fixtures && T.fixtures.embeddingModel;
    await T.test('knowledge', 'search', 'sw.api.knowledge.search(id, query)', {
      sdk:      function () { return sw.api.knowledge.search(kbId, 'test', 5); },
      raw:      { method: 'POST', path: '/knowledge-bases/' + kbId + '/search',
                  body: { query: 'test', limit: 5 } },
      validate: function (r) { T.assert(typeof r === 'object', 'expected object'); },
      skip:     hasEmbedding ? null : 'no embedding provider configured — set up a provider with embedding models'
    });

    await T.test('knowledge', 'crud', 'sw.api.knowledge.del(id)', {
      sdk:      function () { return sw.api.knowledge.del(kbId); },
      raw:      { method: 'DELETE', path: '/knowledge-bases/' + kbId },
      validate: function () { kbId = null; }
    });
  });
})();
