import { describe, expect, it } from 'vitest';
import {
  createAuthedTaskDataSource,
  createSharedTaskDataSource,
} from './task-data-source';

// 純ロジックのみ検証（DOM/hook 非依存）。
// - authed 実装は readonly=false で /api/local/... を選ぶ
// - shared 実装は readonly=true で /api/shared/:token/... を選ぶ
// - Provider 未設置時に useTaskDataSource() が authed 既定へ落ちることは
//   default 値（createAuthedTaskDataSource）で担保する（本体後方互換）。
describe('TaskDataSource', () => {
  describe('authed implementation', () => {
    const ds = createAuthedTaskDataSource();

    it('is not read-only', () => {
      expect(ds.readonly).toBe(false);
    });

    it('builds local subtask raw file URLs', () => {
      expect(ds.subtaskFileRawUrl(7, 'job-1', 'output/a.png')).toBe(
        '/api/local/tasks/7/subtasks/job-1/files/output/a.png',
      );
    });
  });

  describe('shared implementation', () => {
    const ds = createSharedTaskDataSource('tok123');

    it('is read-only', () => {
      expect(ds.readonly).toBe(true);
    });

    it('builds shared subtask raw file URLs scoped to the token', () => {
      expect(ds.subtaskFileRawUrl(7, 'job-1', 'output/a.png')).toBe(
        '/api/shared/tok123/subtasks/job-1/files/output/a.png',
      );
    });

    it('encodes the token in the raw URL', () => {
      const scoped = createSharedTaskDataSource('a/b');
      expect(scoped.subtaskFileRawUrl(1, 'j', 'x.txt')).toBe(
        '/api/shared/a%2Fb/subtasks/j/files/x.txt',
      );
    });
  });
});
