/**
 * Atlas Image Resolver Tests
 * Exercises atlas image reference normalization and resolution against the
 * bundled backend image directory.
 */
const path = require('path');

const {
    createAtlasImageResolver,
    normalizeAtlasImageReference,
} = require('../../utils/atlasImageResolver');

describe('atlasImageResolver', () => {
    const imagesDir = path.join(__dirname, '..', '..', 'public', 'images');
    const resolveAtlasImage = createAtlasImageResolver(imagesDir);

    it('normalizes full public paths to short atlas references', () => {
        expect(
            resolveAtlasImage.toAtlasReference('/public/images/somatoscopy/face/sirka_obliceje_uzka_celek.jpg')
        ).toBe('face/sirka_obliceje_uzka_celek.jpg');

        expect(
            resolveAtlasImage.toAtlasReference('/public/images/somatoscopy/face/paintings/sirka_obliceje_uzka_celek.jpg')
        ).toBe('face/paintings/sirka_obliceje_uzka_celek.jpg');
    });

    it('resolves short atlas references back to public image paths', () => {
        expect(resolveAtlasImage('face/sirka_obliceje_uzka_celek.jpg')).toBe(
            '/public/images/somatoscopy/face/sirka_obliceje_uzka_celek.jpg'
        );

        expect(resolveAtlasImage('face/paintings/sirka_obliceje_uzka_celek.jpg')).toBe(
            '/public/images/somatoscopy/face/paintings/sirka_obliceje_uzka_celek.jpg'
        );
    });

    it('repairs jammed special-subdirectory prefixes when normalizing', () => {
        expect(
            normalizeAtlasImageReference('/public/images/nose/paintingskostena_cast_nosu_stredni_celek.jpg')
        ).toBe('paintings/kostena_cast_nosu_stredni_celek.jpg');
    });

    it('throws for missing files instead of silently keeping broken references', () => {
        expect(() => resolveAtlasImage('paintings/does-not-exist.jpg')).toThrow(
            'Atlas image reference could not be resolved'
        );
    });
});