import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { autoWireStorageAdapter, isAutoWireableStorageProvider } from '../../storage/auto-wire.js';
import { getStorageAdapter, setStorageAdapter } from '../../storage/index.js';
function fakeAdapter() {
    return {
        upload: async () => 'https://cdn.example.com/x',
        download: async () => Buffer.alloc(0),
        delete: async () => { },
        getSignedUrl: async () => 'https://cdn.example.com/x',
    };
}
/** A module importer that returns a platform package exposing `factoryName()`. */
function importerFor(factoryName, adapter) {
    return vi.fn(async (_spec) => ({
        [factoryName]: () => ({ storage: adapter }),
    }));
}
describe('isAutoWireableStorageProvider', () => {
    it('recognizes the known remote shorthands', () => {
        expect(isAutoWireableStorageProvider('vercel-blob')).toBe(true);
        expect(isAutoWireableStorageProvider('s3')).toBe(true);
    });
    it('rejects local / unknown / empty providers', () => {
        expect(isAutoWireableStorageProvider('local')).toBe(false);
        expect(isAutoWireableStorageProvider('gcs')).toBe(false);
        expect(isAutoWireableStorageProvider(undefined)).toBe(false);
    });
});
describe('autoWireStorageAdapter', () => {
    beforeEach(() => setStorageAdapter(null));
    afterEach(() => setStorageAdapter(null));
    it('registers the platform storage adapter for vercel-blob', async () => {
        const importer = importerFor('vercel', fakeAdapter());
        const wired = await autoWireStorageAdapter('vercel-blob', importer);
        expect(wired).toBe(true);
        expect(importer).toHaveBeenCalledWith('@actuate-media/platform-vercel');
        expect(getStorageAdapter()).not.toBeNull();
    });
    it('registers the platform storage adapter for s3 via the aws factory', async () => {
        const importer = importerFor('aws', fakeAdapter());
        const wired = await autoWireStorageAdapter('s3', importer);
        expect(wired).toBe(true);
        expect(importer).toHaveBeenCalledWith('@actuate-media/platform-aws');
        expect(getStorageAdapter()).not.toBeNull();
    });
    it('no-ops for local / unknown providers (no import attempted)', async () => {
        const importer = vi.fn();
        expect(await autoWireStorageAdapter('local', importer)).toBe(false);
        expect(await autoWireStorageAdapter('gcs', importer)).toBe(false);
        expect(await autoWireStorageAdapter(null, importer)).toBe(false);
        expect(importer).not.toHaveBeenCalled();
        expect(getStorageAdapter()).toBeNull();
    });
    it('returns true without importing when an adapter is already registered', async () => {
        setStorageAdapter(fakeAdapter());
        const importer = vi.fn();
        expect(await autoWireStorageAdapter('vercel-blob', importer)).toBe(true);
        expect(importer).not.toHaveBeenCalled();
    });
    it('returns false (never throws) when the platform package fails to load', async () => {
        const importer = vi.fn(async () => {
            throw new Error("Cannot find module '@actuate-media/platform-vercel'");
        });
        expect(await autoWireStorageAdapter('vercel-blob', importer)).toBe(false);
        expect(getStorageAdapter()).toBeNull();
    });
    it('returns false when the package has no usable storage adapter', async () => {
        const importer = importerFor('vercel', undefined);
        expect(await autoWireStorageAdapter('vercel-blob', importer)).toBe(false);
        expect(getStorageAdapter()).toBeNull();
    });
});
//# sourceMappingURL=auto-wire.test.js.map