/**
 * What the widget will accept as a replay configuration.
 *
 * The plugin already refuses to emit a block it cannot fill, so this is the
 * second net rather than the first. It exists because the failure it guards
 * against is invisible: a recording that names no store is one nobody can find
 * in PostHog and nobody can attribute to a merchant — worse than no recording,
 * because it was still taken from a shopper.
 */
import { describe, it, expect } from 'vitest';
import { replaySettings } from './analytics';
import type { WidgetConfig } from './types';

function config( analytics: Record< string, unknown > | undefined ): WidgetConfig {
	return { analytics } as unknown as WidgetConfig;
}

const COMPLETE = {
	replayKey: 'phc_browser',
	ingestHost: 'https://ingest.aisthetix.fyi',
	replayEnabled: true,
	environment: 'staging',
	storeId: 'store_42',
};

describe( 'the replay configuration the widget accepts', () => {
	it( 'accepts a complete block', () => {
		expect( replaySettings( config( COMPLETE ) ) ).toEqual( {
			key: 'phc_browser',
			host: 'https://ingest.aisthetix.fyi',
			enabled: true,
			environment: 'staging',
			store: 'store_42',
		} );
	} );

	it( 'refuses a block with no store', () => {
		expect( replaySettings( config( { ...COMPLETE, storeId: '' } ) ) ).toBeNull();
		expect( replaySettings( config( { ...COMPLETE, storeId: undefined } ) ) ).toBeNull();
	} );

	it( 'refuses a block Core switched off', () => {
		expect( replaySettings( config( { ...COMPLETE, replayEnabled: false } ) ) ).toBeNull();
	} );

	it( 'refuses a block with no key or no host', () => {
		expect( replaySettings( config( { ...COMPLETE, replayKey: '' } ) ) ).toBeNull();
		expect( replaySettings( config( { ...COMPLETE, ingestHost: '' } ) ) ).toBeNull();
	} );

	it( 'refuses a config with no analytics block at all', () => {
		expect( replaySettings( config( undefined ) ) ).toBeNull();
	} );
} );
