import {
  describe,
  it,
  expect
} from 'vitest';
import type { Message } from '../../../../shared-kernel';
import { shouldShowMessageEditedLabel } from './message.rules';

function createMessage(overrides: Partial<Message> = {}): Message {
  return {
    id: 'message-1',
    roomId: 'room-1',
    senderId: 'user-1',
    senderName: 'User 1',
    content: 'hello',
    timestamp: 1_000,
    reactions: [],
    isDeleted: false,
    ...overrides
  };
}

describe('message.rules', () => {
  describe('shouldShowMessageEditedLabel', () => {
    it('returns false for newly created messages without an edit revision', () => {
      expect(shouldShowMessageEditedLabel(createMessage())).toBe(false);
    });

    it('returns false when editedAt equals the original timestamp (legacy create rows)', () => {
      expect(shouldShowMessageEditedLabel(createMessage({
        editedAt: 1_000,
        timestamp: 1_000,
        revision: 0
      }))).toBe(false);
    });

    it('returns true when a message has an edit revision', () => {
      expect(shouldShowMessageEditedLabel(createMessage({
        editedAt: 2_000,
        revision: 1
      }))).toBe(true);
    });

    it('returns true for legacy edited messages with editedAt after timestamp', () => {
      expect(shouldShowMessageEditedLabel(createMessage({
        editedAt: 2_000,
        timestamp: 1_000
      }))).toBe(true);
    });

    it('returns false for deleted messages even when editedAt is set', () => {
      expect(shouldShowMessageEditedLabel(createMessage({
        editedAt: 2_000,
        revision: 1,
        isDeleted: true
      }))).toBe(false);
    });
  });
});
