import { describe, expect, it } from 'vitest';
import { settled } from './settled';

describe('settled', () => {
	it('returns the data for a fulfilled ok response', () => {
		const res: PromiseSettledResult<unknown> = {
			status: 'fulfilled',
			value: { response: { ok: true } as Response, data: [{ id: '1' }] }
		};
		expect(settled<{ id: string }[]>(res)).toEqual([{ id: '1' }]);
	});

	it('returns null for a fulfilled non-ok response', () => {
		const res: PromiseSettledResult<unknown> = {
			status: 'fulfilled',
			value: { response: { ok: false, status: 403 } as Response, data: undefined }
		};
		expect(settled(res)).toBeNull();
	});

	it('returns null for a rejected result', () => {
		const res: PromiseSettledResult<unknown> = {
			status: 'rejected',
			reason: new Error('network error')
		};
		expect(settled(res)).toBeNull();
	});

	it('returns null for undefined input', () => {
		expect(settled(undefined)).toBeNull();
	});

	it('returns null for a fulfilled null value (Promise.resolve(null) placeholder slot)', () => {
		const res: PromiseSettledResult<unknown> = { status: 'fulfilled', value: null };
		expect(settled(res)).toBeNull();
	});
});
