import test from 'node:test';
import assert from 'node:assert/strict';

import { DirectusClient } from '../js/services/directus-client.js';

test('requests berkas with expanded Directus file metadata', () => {
    const url = new URL(DirectusClient.itemsUrl('berkas'));

    assert.equal(url.pathname, '/items/berkas');
    assert.equal(url.searchParams.get('limit'), '-1');
    assert.equal(url.searchParams.get('fields'), '*,file.id,file.filename_download,file.type,file.filename_disk');
});

test('builds asset and viewer URLs from expanded Directus file objects', () => {
    const item = {
        nama_berkas: 'Panduan Penulisan Proposal Kerja Praktek',
        file: {
            id: '4976330e-e547-45f6-b9be-afce493c8a82',
            filename_download: 'Panduan Penulisan Proposal Kerja Praktek.docx',
            type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
        },
    };

    assert.equal(
        DirectusClient.assetUrl(item.file),
        'https://api.ifuntanhub.dev/assets/4976330e-e547-45f6-b9be-afce493c8a82',
    );

    const viewerUrl = new URL(DirectusClient.viewerUrl(item), 'https://example.test/groups/Tapops/');
    assert.equal(viewerUrl.pathname, '/groups/Tapops/viewer.html');
    assert.equal(viewerUrl.searchParams.get('id'), '4976330e-e547-45f6-b9be-afce493c8a82');
    assert.equal(viewerUrl.searchParams.get('name'), 'Panduan Penulisan Proposal Kerja Praktek');
    assert.equal(viewerUrl.searchParams.get('type'), 'docx');
});
