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

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

    // ── List ──

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

    // ── Create ──

    await T.test('notes', 'crud', 'sw.api.notes.create()', {
      sdk: function () {
        return sw.api.notes.create({ title: tag + '-note', content: '# Test\nBody text' });
      },
      raw: { method: 'POST', path: '/notes', body: { title: tag + '-note', content: '# Test\nBody text' } },
      validate: function (r) {
        T.assertShape(r, T.S.note, 'note');
        noteId = r.id;
        T.registerCleanup(function () { if (noteId) return T.safeDelete('/notes/' + noteId); });
      }
    });

    if (!noteId) return;

    // ── Get ──

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

    // ── Update ──

    await T.test('notes', 'crud', 'sw.api.notes.update(id, data)', {
      sdk:      function () { return sw.api.notes.update(noteId, { title: tag + '-note-upd', content: 'updated' }); },
      raw:      { method: 'PUT', path: '/notes/' + noteId, body: { title: tag + '-note-upd', content: 'updated' } },
      validate: function (r) {
        T.assertShape(r, T.S.note, 'note');
      }
    });

    // ── Search ──

    await T.test('notes', 'search', 'sw.api.notes.search(query)', {
      sdk:      function () { return sw.api.notes.search(tag, 5); },
      raw:      { method: 'GET', path: '/notes/search?q=' + encodeURIComponent(tag) + '&limit=5' },
      validate: function (r) { var arr = T.unwrapList(r); T.assert(arr.length >= 0, 'expected list'); }
    });

    // ── Search Titles ──

    await T.test('notes', 'search', 'sw.api.notes.searchTitles(query)', {
      sdk:      function () { return sw.api.notes.searchTitles(tag, 5); },
      raw:      { method: 'GET', path: '/notes/search-titles?q=' + encodeURIComponent(tag) + '&limit=5' },
      validate: function (r) { var arr = T.unwrapList(r); T.assert(arr.length >= 0, 'expected list'); }
    });

    // ── Folders ──

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

    // ── Backlinks ──

    await T.test('notes', 'links', 'sw.api.notes.backlinks(id)', {
      sdk:      function () { return sw.api.notes.backlinks(noteId); },
      raw:      { method: 'GET', path: '/notes/' + noteId + '/backlinks' },
      validate: function (r) { var arr = T.unwrapList(r); T.assert(arr.length >= 0, 'expected list'); }
    });

    // ── Graph ──

    await T.test('notes', 'graph', 'sw.api.notes.graph()', {
      sdk:      function () { return sw.api.notes.graph(); },
      raw:      { method: 'GET', path: '/notes/graph' },
      validate: function (r) { T.assert(typeof r === 'object' || Array.isArray(r), 'expected object or array'); }
    });

    // ── Delete ──

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