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

const tasks = [
  { id: 1, ownerId: 'alice' },
  { id: 2, ownerId: 'bob' },
  { id: 3, ownerId: 'alice' },
  { id: 4, ownerId: null },       // legacy no-auth row
  { id: 5 },                       // ownerId undefined
];

describe('filterTasksByScope', () => {
  it("scope='mine' keeps only the current user's tasks", () => {
    expect(filterTasksByScope(tasks, 'mine', 'alice').map(t => t.id)).toEqual([1, 3]);
  });

  it("scope='others' keeps everyone else's tasks (incl. legacy null owners)", () => {
    expect(filterTasksByScope(tasks, 'others', 'alice').map(t => t.id)).toEqual([2, 4, 5]);
  });

  it('mine + others partition the full list with no overlap', () => {
    const mine = filterTasksByScope(tasks, 'mine', 'alice').map(t => t.id);
    const others = filterTasksByScope(tasks, 'others', 'alice').map(t => t.id);
    expect([...mine, ...others].sort()).toEqual([1, 2, 3, 4, 5]);
    expect(mine.filter(id => others.includes(id))).toEqual([]);
  });

  it('no current user (auth disabled) returns everything for either scope', () => {
    expect(filterTasksByScope(tasks, 'mine', null)).toHaveLength(5);
    expect(filterTasksByScope(tasks, 'others', null)).toHaveLength(5);
  });

  it('legacy null/undefined owners never match mine', () => {
    expect(filterTasksByScope(tasks, 'mine', 'bob').map(t => t.id)).toEqual([2]);
  });
});
