import { beforeEach, describe, expect, test } from 'vitest';
import { createSnippet } from '@core/snippet';
import { useNotificationStore } from '../stores/NotificationStore';
import { useSnippetStore } from '../stores/SnippetStore';
import { publishActiveSnippet } from './snippet-actions';

const T = new Date('2026-06-01T00:00:00Z');
const snippets = () => useSnippetStore.getState();
const toasts = () => useNotificationStore.getState().notifications;

beforeEach(() => {
  useSnippetStore.getState().reset();
  useNotificationStore.getState().clear();
});

describe('publishActiveSnippet', () => {
  test('publishes the active draft and raises one success toast', () => {
    snippets().hydrate([createSnippet({ id: 'a', now: T })], 'a');
    snippets().updateDraft('{"published":1}');

    publishActiveSnippet();

    expect(snippets().snippets.find((s) => s.id === 'a')?.spec).toBe('{"published":1}');
    expect(toasts()).toHaveLength(1);
    expect(toasts()[0]).toMatchObject({ kind: 'success', title: 'Snippet published' });
  });

  test('is a silent no-op when no snippet is active', () => {
    snippets().hydrate([], null);

    publishActiveSnippet();

    expect(toasts()).toHaveLength(0);
  });
});
