import assert from 'node:assert/strict';
import test from 'node:test';

import { selectWorkspaceCollections } from './workspace-v22-source';

const appData = {
  plans: [{ id: 'app-plan' }] as any[],
  devTasks: [{ id: 'app-dev' }] as any[],
  testCases: [{ id: 'app-test' }] as any[],
  bugs: [{ id: 'app-bug' }] as any[],
};

test('selectWorkspaceCollections prefers successfully loaded V2.2 data even when it is empty', () => {
  const selected = selectWorkspaceCollections({
    v22Loaded: true,
    v22Failed: false,
    v22Data: { versionPlans: [], devTasks: [], testCases: [], bugs: [] },
    appData,
  });

  assert.deepEqual(selected, { versionPlans: [], devTasks: [], testCases: [], bugs: [] });
});

test('selectWorkspaceCollections falls back to AppData only when V2.2 is unavailable', () => {
  const selected = selectWorkspaceCollections({
    v22Loaded: false,
    v22Failed: true,
    v22Data: null,
    appData,
  });

  assert.deepEqual(selected, {
    versionPlans: appData.plans,
    devTasks: appData.devTasks,
    testCases: appData.testCases,
    bugs: appData.bugs,
  });
});
