import test from 'node:test';
import assert from 'node:assert/strict';
import { resolveNotificationAggregateResult, withInferredNotificationOriginData } from './notification-service.js';

test('resolveNotificationAggregateResult marks managed-service web failures as failed when iOS is disabled', () => {
  const result = resolveNotificationAggregateResult(
    {
      ios: { ok: true, skipped: true },
      web: { ok: false, skipped: false },
    },
    {
      disableIos: true,
    },
  );

  assert.deepEqual(result, {
    ok: false,
    skipped: false,
  });
});

test('resolveNotificationAggregateResult treats fully skipped enabled channels as skipped success', () => {
  const result = resolveNotificationAggregateResult(
    {
      ios: { ok: true, skipped: true },
      web: { ok: true, skipped: true },
    },
    {
      disableIos: true,
    },
  );

  assert.deepEqual(result, {
    ok: true,
    skipped: true,
  });
});

test('notification payload infers app origin metadata from target filters when missing', () => {
  const resolved = withInferredNotificationOriginData({
    title: 'Codex Live test',
    body: 'body',
    data: {},
    targetAppOrigins: ['https://test.sm-home.cloud'],
  } as any);

  assert.equal(resolved.data.appOrigin, 'https://test.sm-home.cloud');
  assert.equal(resolved.data.appDomain, 'test.sm-home.cloud');
});

test('notification payload infers app domain metadata from target filters when only domain is provided', () => {
  const resolved = withInferredNotificationOriginData({
    title: 'Codex Live test',
    body: 'body',
    data: {},
    targetAppDomains: ['test.sm-home.cloud'],
  } as any);

  assert.equal(resolved.data.appOrigin, undefined);
  assert.equal(resolved.data.appDomain, 'test.sm-home.cloud');
});

test('notification payload keeps explicit app origin metadata when already present', () => {
  const resolved = withInferredNotificationOriginData({
    title: 'Codex Live test',
    body: 'body',
    data: {
      appOrigin: 'https://preview.sm-home.cloud',
      appDomain: 'preview.sm-home.cloud',
    },
    targetAppOrigins: ['https://test.sm-home.cloud'],
    targetAppDomains: ['test.sm-home.cloud'],
  } as any);

  assert.equal(resolved.data.appOrigin, 'https://preview.sm-home.cloud');
  assert.equal(resolved.data.appDomain, 'preview.sm-home.cloud');
});
