import { describe, expect, it } from 'bun:test';
import { ENCRYPTION_CONTROL_AUDIENCE_PATH } from '../../src/core/constants.js';
import { EncryptionControl } from '../../src/core/encryption-control.js';
import { DwnInterfaceName, DwnMethodName } from '../../src/index.js';
const tenant = 'did:example:tenant';
describe('EncryptionControl', () => {
    describe('canRead()', () => {
        it('returns true for non-control records', async () => {
            const result = await EncryptionControl.canRead({
                tenant,
                incomingMessage: makeIncomingMessage(),
                requester: 'did:example:alice',
                recordsWriteMessage: makeRecordsWriteMessage({ protocolPath: 'thread/message' }),
                validationStateReader: {},
            });
            expect(result).toBe(true);
        });
        it('returns true when the requester is the tenant', async () => {
            const result = await EncryptionControl.canRead({
                tenant,
                incomingMessage: makeIncomingMessage(),
                requester: tenant,
                recordsWriteMessage: makeRecordsWriteMessage({ protocolPath: ENCRYPTION_CONTROL_AUDIENCE_PATH }),
                validationStateReader: {},
            });
            expect(result).toBe(true);
        });
    });
    describe('projectCurrentAudienceRecords()', () => {
        it('should reuse current audience resolution results from a caller-provided cache', async () => {
            const current = makeAudienceRecordsWriteMessage({
                dateCreated: '2026-01-01T00:00:00.000000Z',
                keyId: 'current',
                recordId: 'current-record',
            });
            const loser = makeAudienceRecordsWriteMessage({
                dateCreated: '2026-01-01T00:00:01.000000Z',
                keyId: 'loser',
                recordId: 'loser-record',
            });
            let queryCount = 0;
            const messageStore = {
                query: async () => {
                    queryCount++;
                    return { messages: [current, loser] };
                },
            };
            const currentAudienceRecordIdCache = new Map();
            const loserProjection = await EncryptionControl.projectCurrentAudienceRecords({
                currentAudienceRecordIdCache,
                messageStore,
                tenant,
                recordsWriteMessages: [loser],
            });
            expect(loserProjection).toEqual([]);
            const currentProjection = await EncryptionControl.projectCurrentAudienceRecords({
                currentAudienceRecordIdCache,
                messageStore,
                tenant,
                recordsWriteMessages: [current],
            });
            expect(currentProjection.map(record => record.recordId)).toEqual(['current-record']);
            expect(queryCount).toBe(1);
        });
    });
});
function makeIncomingMessage() {
    return {
        descriptor: {
            interface: DwnInterfaceName.Records,
            method: DwnMethodName.Read,
        },
    };
}
function makeRecordsWriteMessage(options) {
    return {
        descriptor: {
            protocolPath: options.protocolPath,
        },
    };
}
function makeAudienceRecordsWriteMessage(options) {
    return {
        descriptor: {
            interface: DwnInterfaceName.Records,
            method: DwnMethodName.Write,
            protocol: 'http://example.com/protocol',
            protocolPath: ENCRYPTION_CONTROL_AUDIENCE_PATH,
            dateCreated: options.dateCreated,
            tags: {
                contextId: '',
                keyId: options.keyId,
                protocol: 'http://example.com/protocol',
                rolePath: 'member',
            },
        },
        recordId: options.recordId,
    };
}
//# sourceMappingURL=encryption-control.spec.js.map