"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 ORG_DIR = path.join(os.homedir(), '.fivo', 'cell', 'orgs');
describe('OrganizationMemory', () => {
    let module;
    beforeAll(async () => {
        module = await Promise.resolve().then(() => __importStar(require('../organization-memory')));
    });
    beforeEach(() => {
        const fs = require('fs');
        if (fs.existsSync(ORG_DIR)) {
            for (const f of fs.readdirSync(ORG_DIR).filter((n) => n.endsWith('.json'))) {
                fs.unlinkSync(path.join(ORG_DIR, f));
            }
        }
    });
    test('createOrganization creates with admin member', () => {
        const org = module.createOrganization('TestOrg', 'dev1');
        expect(org.name).toBe('TestOrg');
        expect(org.members).toHaveLength(1);
        expect(org.members[0].role).toBe('admin');
    });
    test('getOrganization returns null for missing', () => {
        expect(module.getOrganization('nonexistent')).toBeNull();
    });
    test('addMember only works for admin', () => {
        module.createOrganization('Acme', 'admin1');
        const result = module.addMember('acme', 'user1', 'member', 'unknown');
        expect(result).toBeNull();
        const ok = module.addMember('acme', 'user1', 'member', 'admin1');
        expect(ok).not.toBeNull();
        expect(ok.members).toHaveLength(2);
    });
    test('removeMember cannot remove self', () => {
        module.createOrganization('Corp', 'admin1');
        const result = module.removeMember('corp', 'admin1', 'admin1');
        expect(result).toBeNull();
    });
    test('createTeam creates team with creator as member', () => {
        module.createOrganization('TeamOrg', 'admin1');
        const org = module.createTeam('teamorg', 'Backend', 'BE team', 'admin1');
        const team = org.teams.find((t) => t.name === 'Backend');
        expect(team).toBeDefined();
        expect(team.memberDeviceIds).toContain('admin1');
    });
    test('addToTeam adds member to team', () => {
        module.createOrganization('TeamOrg2', 'admin1');
        module.createTeam('teamorg2', 'Frontend', 'FE team', 'admin1');
        const org = module.addToTeam('teamorg2', 'frontend', 'user2', 'admin1');
        const team = org.teams.find((t) => t.id === 'frontend');
        expect(team.memberDeviceIds).toContain('user2');
    });
});
//# sourceMappingURL=organization-memory.test.js.map