// Standalone operational tools intentionally live outside Nx project boundaries.
// eslint-disable-next-line @nx/enforce-module-boundaries
import {
	basicAuthHeader,
	buildCreateBookingRequest,
	runBookingServerE2e,
} from '../../../../../../../../../tools/rwg/booking-server-e2e'

describe('booking-server sandbox E2E tool logic', () => {
	it.each([
		{},
		{ GOOGLE_ACTION_CENTER_BOOKING_SERVER_USERNAME: 'user' },
		{ GOOGLE_ACTION_CENTER_BOOKING_SERVER_PASSWORD: 'password' },
		{ GOOGLE_ACTION_CENTER_BOOKING_SERVER_USERNAME: ' ', GOOGLE_ACTION_CENTER_BOOKING_SERVER_PASSWORD: 'password' },
	])('skips without complete, non-empty credentials and does not call the network', async (env) => {
		const fetchStub = jest.fn()
		const log = jest.fn()

		await expect(
			runBookingServerE2e(env, { fetch: fetchStub, log, now: () => new Date(0), uuid: () => 'unused' }),
		).resolves.toBe('skipped')
		expect(fetchStub).not.toHaveBeenCalled()
		expect(log).toHaveBeenCalledWith('BOOKING_SERVER_E2E_READY_FOR_CREDENTIALS')
	})

	it('builds Basic Auth from supplied environment-derived values', () => {
		expect(basicAuthHeader({ username: 'sandbox-user', password: 'sandbox-password' })).toBe(
			`Basic ${Buffer.from('sandbox-user:sandbox-password').toString('base64')}`,
		)
	})

	it('reuses the identical idempotency token and request body for a retry', () => {
		const slot = {
			merchant_id: 'configurable-merchant',
			service_id: 'configurable-service',
			start_sec: '1800000000',
			duration_sec: '7200',
			resources: { party_size: 2 },
		}
		const first = buildCreateBookingRequest(slot, 'fixed-test-token')
		const retry = first

		expect(retry).toBe(first)
		expect(retry).toMatchObject({ idempotency_token: 'fixed-test-token', slot })
	})
})
