import { describe, expect, it } from 'vitest';
import { listTrashed } from '../vault';
import type { Manifest } from '../../shared/types';

function makeManifest(items: Record<string, { trashed_at?: number }>): Manifest {
  const manifest: Manifest = { schema_version: 2, items: {} };
  for (const [id, { trashed_at }] of Object.entries(items)) {
    manifest.items[id] = {
      id,
      type: 'login',
      title: `Item ${id}`,
      tags: [],
      favorite: false,
      modified: 1000,
      trashed_at,
      attachment_summaries: [],
    };
  }
  return manifest;
}

describe('listTrashed', () => {
  it('returns empty array when no trashed items', () => {
    const manifest = makeManifest({ a: {}, b: {} });
    expect(listTrashed(manifest)).toEqual([]);
  });

  it('filters to only trashed items', () => {
    const manifest = makeManifest({
      a: {},
      b: { trashed_at: 1000 },
      c: { trashed_at: 2000 },
    });
    const result = listTrashed(manifest);
    expect(result).toHaveLength(2);
    expect(result.map(([id]) => id)).toEqual(['c', 'b']); // sorted newest first
  });

  it('sorts by trashed_at descending', () => {
    const manifest = makeManifest({
      old: { trashed_at: 100 },
      mid: { trashed_at: 500 },
      new: { trashed_at: 900 },
    });
    const result = listTrashed(manifest);
    expect(result.map(([id]) => id)).toEqual(['new', 'mid', 'old']);
  });
});
