Repository: directus/directus
File: api/src/telemetry/lib/get-report.test.ts (TypeScript)

Code before:
	vi.mocked(getDatabaseClient).mockReturnValue('test-db' as any);

	const report = await getReport();

	expect(report.url).toBe(mockEnv['PUBLIC_URL']);
	expect(report.database).toBe('test-db');
	expect(report.version).toBe(version);
});

test('Runs and returns basic counts', async () => {
	const mockItemCount = {
		directus_dashboards: 15,
		directus_extensions: 30,
		directus_files: 45,
		directus_flows: 60,
		directus_roles: 75,
		directus_shares: 90,
	};

	vi.mocked(getItemCount).mockResolvedValue(mockItemCount);

	const report = await getReport();

	expect(getItemCount).toHaveBeenCalledWith(mockDb, [
		{ collection: 'directus_dashboards' },
		{ collection: 'directus_extensions', where: ['enabled', '=', true] },
		{ collection: 'directus_files' },
		{ collection: 'directus_flows', where: ['status', '=', 'active'] },
		{ collection: 'directus_roles' },
		{ collection: 'directus_shares' },
	]);

	expect(report.dashboards).toBe(mockItemCount.directus_dashboards);
	expect(report.extensions).toBe(mockItemCount.directus_extensions);
	expect(report.files).toBe(mockItemCount.directus_files);
	expect(report.flows).toBe(mockItemCount.directus_flows);
	expect(report.roles).toBe(mockItemCount.directus_roles);
	expect(report.shares).toBe(mockItemCount.directus_shares);
});

test('Runs and returns user counts', async () => {
	const report = await getReport();

	expect(getUserCount).toHaveBeenCalledWith(mockDb);

	expect(report.admin_users).toBe(mockUserCounts.admin);
	expect(report.app_users).toBe(mockUserCounts.app);
	expect(report.api_users).toBe(mockUserCounts.api);
});

test('Runs and returns user item counts', async () => {

Review comment (bug):
Its not because of this PR but I noticed that we are actually counting bundles slightly wrong. Because a bundle is just a row and the inside-extensions point to it via its `id` a bundle with one extension inside counts as 2 and a bundle with two extensions inside counts as 3 - etc.

I looked up the original PR for the `get-item-count.ts` file, [here](https://github.com/directus/directus/pull/20825), which already introduced this behavior but I dont see it being discussed there. Did we decide that we are OK with this somewhere else? (cc @rijkvanzanten maybe you know?)

Now that we added a possibility for a where clause we might be able to filter those out! I personally would be in favor of doing so. :smile: :+1: 

Code after:
	vi.mocked(getUserItemCount).mockResolvedValue(mockUserItemCounts);
	vi.mocked(getFieldCount).mockResolvedValue(mockFieldCounts);
	vi.mocked(getExtensionCount).mockResolvedValue(mockExtensionCounts);
});

afterEach(() => {
	vi.clearAllMocks();
});

test('Returns environment information', async () => {
	vi.mocked(getDatabaseClient).mockReturnValue('test-db' as any);

	const report = await getReport();

	expect(report.url).toBe(mockEnv['PUBLIC_URL']);
	expect(report.database).toBe('test-db');
	expect(report.version).toBe(version);
});

test('Runs and returns basic counts', async () => {
	const mockItemCount = {
		directus_dashboards: 15,
		directus_files: 45,
		directus_flows: 60,
		directus_roles: 75,
		directus_shares: 90,
	};

	vi.mocked(getItemCount).mockResolvedValue(mockItemCount);

	const report = await getReport();

	expect(getItemCount).toHaveBeenCalledWith(mockDb, [
		{ collection: 'directus_dashboards' },
		{ collection: 'directus_files' },
		{ collection: 'directus_flows', where: ['status', '=', 'active'] },
		{ collection: 'directus_roles' },
		{ collection: 'directus_shares' },
	]);

	expect(report.dashboards).toBe(mockItemCount.directus_dashboards);
	expect(report.files).toBe(mockItemCount.directus_files);
	expect(report.flows).toBe(mockItemCount.directus_flows);
	expect(report.roles).toBe(mockItemCount.directus_roles);
	expect(report.shares).toBe(mockItemCount.directus_shares);
});

test('Runs and returns user counts', async () => {
	const report = await getReport();

	expect(getUserCount).toHaveBeenCalledWith(mockDb);