"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
const path = __importStar(require("path"));
const os = __importStar(require("os"));
const PKG_DIR = path.join(os.homedir(), '.fivo', 'cell', 'marketplace');
describe('Marketplace', () => {
    let module;
    beforeAll(async () => {
        module = await Promise.resolve().then(() => __importStar(require('../../walls/05-community/share/marketplace')));
    });
    beforeEach(() => {
        // Clean test dir
        const fs = require('fs');
        if (fs.existsSync(PKG_DIR)) {
            for (const f of fs.readdirSync(PKG_DIR).filter((n) => n.endsWith('.json'))) {
                fs.unlinkSync(path.join(PKG_DIR, f));
            }
        }
    });
    test('createPackage returns package with id and version', () => {
        const pkg = module.createPackage({ name: 'test-pkg', description: 'Test', author: 'dev1', patterns: [{ category: 'a', description: 'b', from: 'x', to: 'y' }], tags: ['test'] });
        expect(pkg.id).toBe('test-pkg');
        expect(pkg.version).toBe(1);
        expect(pkg.author).toBe('dev1');
    });
    test('getPackage returns null for missing', () => {
        expect(module.getPackage('nonexistent')).toBeNull();
    });
    test('searchPackages finds by name, description, and tags', () => {
        module.createPackage({ name: 'react-hook', description: 'React hook for X', author: 'dev1', patterns: [], tags: ['react', 'hook'] });
        expect(module.searchPackages('react').length).toBeGreaterThanOrEqual(1);
        expect(module.searchPackages('hook').length).toBeGreaterThanOrEqual(1);
        expect(module.searchPackages('X').length).toBeGreaterThanOrEqual(1);
    });
    test('downloadPackage increments download count', () => {
        module.createPackage({ name: 'dl-pkg', description: 'Test DL', author: 'dev1', patterns: [], tags: [] });
        const before = module.getPackage('dl-pkg');
        module.downloadPackage('dl-pkg');
        const after = module.getPackage('dl-pkg');
        expect(after.downloads).toBe(before.downloads + 1);
    });
    test('ratePackage updates rating', () => {
        module.createPackage({ name: 'rate-pkg', description: 'Test Rate', author: 'dev1', patterns: [], tags: [] });
        module.ratePackage('rate-pkg', 'dev2', 5, 'Great!');
        module.ratePackage('rate-pkg', 'dev3', 3);
        const pkg = module.getPackage('rate-pkg');
        expect(pkg.ratingCount).toBe(2);
        expect(pkg.rating).toBe(4);
    });
    test('ratePackage rejects invalid rating', () => {
        module.createPackage({ name: 'bad-rate', description: 'Test', author: 'dev1', patterns: [], tags: [] });
        expect(module.ratePackage('bad-rate', 'dev2', 0)).toBeNull();
        expect(module.ratePackage('bad-rate', 'dev2', 6)).toBeNull();
    });
    test('getReviews returns reviews for a package', () => {
        module.createPackage({ name: 'rev-pkg', description: 'Test Rev', author: 'dev1', patterns: [], tags: [] });
        module.ratePackage('rev-pkg', 'dev2', 4, 'Nice!');
        const reviews = module.getReviews('rev-pkg');
        expect(reviews.length).toBe(1);
        expect(reviews[0].comment).toBe('Nice!');
    });
});
//# sourceMappingURL=marketplace.test.js.map