import assert from 'node:assert/strict';
import { describe, it } from 'node:test';
import {
  SESSION_EXPIRED_MESSAGE,
  shouldDeferBackendFailToCaller,
  shouldSuppressErrorMessage
} from '../src/service/request/error-message';

describe('request session expired message handling', () => {
  it('uses the unified session expired message', () => {
    assert.equal(SESSION_EXPIRED_MESSAGE, '登录已失效，请重新登录');
  });

  it('suppresses raw backend errors when the request is marked silent', () => {
    assert.equal(
      shouldSuppressErrorMessage({
        backendErrorCode: '500',
        suppressErrorMessage: true,
        logoutCodes: [],
        modalLogoutCodes: [],
        expiredTokenCodes: []
      }),
      true
    );
  });

  it('lets refresh-token callers handle every backend failure silently', () => {
    assert.equal(
      shouldDeferBackendFailToCaller({
        suppressErrorMessage: true,
        skipTokenRefresh: true
      }),
      true
    );
  });

  it('suppresses auth lifecycle error codes handled by request flow', () => {
    assert.equal(
      shouldSuppressErrorMessage({
        backendErrorCode: '401',
        logoutCodes: ['401'],
        modalLogoutCodes: [],
        expiredTokenCodes: []
      }),
      true
    );
    assert.equal(
      shouldSuppressErrorMessage({
        backendErrorCode: '7777',
        logoutCodes: [],
        modalLogoutCodes: ['7777'],
        expiredTokenCodes: []
      }),
      true
    );
    assert.equal(
      shouldSuppressErrorMessage({
        backendErrorCode: '1002023000',
        logoutCodes: [],
        modalLogoutCodes: [],
        expiredTokenCodes: ['1002023000']
      }),
      true
    );
  });

  it('keeps normal backend errors visible', () => {
    assert.equal(
      shouldSuppressErrorMessage({
        backendErrorCode: '500',
        logoutCodes: ['401'],
        modalLogoutCodes: ['7777'],
        expiredTokenCodes: ['1002023000']
      }),
      false
    );
  });
});
