import { describe, expect, it } from 'bun:test';
import { ChatHistoryRestoreTarget } from '../types/terminal';

const VALID_TARGETS = new Set<ChatHistoryRestoreTarget>(['activeTab', 'newTab']);

const validateRestoreTarget = (value: unknown): ChatHistoryRestoreTarget => {
  if (typeof value === 'string' && VALID_TARGETS.has(value as ChatHistoryRestoreTarget)) {
    return value as ChatHistoryRestoreTarget;
  }
  return 'activeTab';
};

describe('workspace preferences validation', () => {
  it('defaults to activeTab', () => {
    expect(validateRestoreTarget(undefined)).toBe('activeTab');
    expect(validateRestoreTarget(null)).toBe('activeTab');
    expect(validateRestoreTarget('')).toBe('activeTab');
  });

  it('accepts activeTab and newTab', () => {
    expect(validateRestoreTarget('activeTab')).toBe('activeTab');
    expect(validateRestoreTarget('newTab')).toBe('newTab');
  });

  it('ignores invalid persisted values', () => {
    expect(validateRestoreTarget('invalid')).toBe('activeTab');
    expect(validateRestoreTarget('ACTIVETAB')).toBe('activeTab');
    expect(validateRestoreTarget(42)).toBe('activeTab');
    expect(validateRestoreTarget({})).toBe('activeTab');
  });
});
