import { describe, it, expect } from 'vitest';
import { hasAnyDocuments } from './tagUtils';
import type { components } from '$lib/generated/api';

type TagTreeNodeDTO = components['schemas']['TagTreeNodeDTO'];

function makeNode(
	documentCount: number,
	subtreeDocumentCount: number,
	children: TagTreeNodeDTO[] = []
): TagTreeNodeDTO {
	return { id: 'id', name: 'name', documentCount, subtreeDocumentCount, children };
}

describe('hasAnyDocuments', () => {
	it('returns false for a node whose subtree holds no documents', () => {
		expect(hasAnyDocuments(makeNode(0, 0))).toBe(false);
	});

	it('returns true for a node whose subtree holds documents', () => {
		expect(hasAnyDocuments(makeNode(3, 3))).toBe(true);
	});

	it('keys on the subtree rollup, not direct documentCount: 0 direct but rollup 5 → true', () => {
		// The rollup already includes descendants — a single field read, no recursion over children.
		expect(hasAnyDocuments(makeNode(0, 5))).toBe(true);
	});

	it('keys on the subtree rollup, not direct documentCount: 5 direct but rollup 0 → false', () => {
		expect(hasAnyDocuments(makeNode(5, 0))).toBe(false);
	});
});
