import assert from 'node:assert/strict';
import test from 'node:test';
import { isLegacyChatShareTokenRowNeedingMigration } from './shared-resource-token-service.js';

const completeSnapshot = {
  id: 'token-setting',
  name: 'Token Setting',
  defaultExpiresInMinutes: 60,
  maxTokensPer30Days: 0,
  maxTokensPer7Days: 0,
  maxTokensPer5Hours: 0,
  oneTimeTokenLimit: 0,
  allowedAppIds: [],
};

const completeContext = {
  kind: 'request-bundle',
  sessionId: 'session-1',
  requestId: 'request-1',
};

test('isLegacyChatShareTokenRowNeedingMigration flags rows with legacy token_setting_id', () => {
  assert.equal(
    isLegacyChatShareTokenRowNeedingMigration({
      token_setting_id: 'legacy-setting',
      token_setting_snapshot_json: completeSnapshot,
      resource_context_json: completeContext,
      allowed_app_ids_json: '[]',
    }),
    true,
  );
});

test('isLegacyChatShareTokenRowNeedingMigration flags rows with missing resource context', () => {
  assert.equal(
    isLegacyChatShareTokenRowNeedingMigration({
      token_setting_id: null,
      token_setting_snapshot_json: completeSnapshot,
      resource_context_json: null,
      allowed_app_ids_json: '[]',
    }),
    true,
  );
});

test('isLegacyChatShareTokenRowNeedingMigration keeps valid current rows even when allowed apps are empty', () => {
  assert.equal(
    isLegacyChatShareTokenRowNeedingMigration({
      token_setting_id: null,
      token_setting_snapshot_json: completeSnapshot,
      resource_context_json: completeContext,
      allowed_app_ids_json: '[]',
    }),
    false,
  );
});
