<commit_before>import { module, test } from 'qunit';
import { click, visit, currentURL, waitFor } from '@ember/test-helpers';
import { setupApplicationTest } from 'ember-qunit';
import { setupMirage } from 'ember-cli-mirage/test-support';
import { MirageTestContext } from 'ember-cli-mirage/test-support';
import Layer2TestWeb3Strategy from '@cardstack/web-client/utils/web3-strategies/test-layer2';
import WorkflowPersistence from '@cardstack/web-client/services/workflow-persistence';
import { MerchantSafe, PrepaidCardSafe } from '@cardstack/cardpay-sdk';
import { buildState } from '@cardstack/web-client/models/workflow/workflow-session';
import { setupHubAuthenticationToken } from '../helpers/setup';

interface Context extends MirageTestContext {}

module('Acceptance | create merchant persistence', function (hooks) {
  setupApplicationTest(hooks);
  setupMirage(hooks);
  setupHubAuthenticationToken(hooks);
  let workflowPersistenceService: WorkflowPersistence;
  let layer2AccountAddress = '0x182619c6Ea074C053eF3f1e1eF81Ec8De6Eb6E44';
  const prepaidCardAddress = '0x81c89274Dc7C9BAcE082d2ca00697d2d2857D2eE';
  const prepaidCard: PrepaidCardSafe = {
    type: 'prepaid-card',
    address: prepaidCardAddress,
    customizationDID: 'did:cardstack:1pkYh9uJHdfMJZt4mURGmhps96b157a2d744efd9',
    reloadable: false,
    spendFaceValue: 500,
    transferrable: true,
    createdAt: Date.now() / 1000,
    tokens: [],
    owners: [layer2AccountAddress],
    issuingToken: '<PASSWORD>',
    prepaidCardOwner: layer2AccountAddress,
    hasBeenUsed: false,
    issuer: layer2AccountAddress,
  };
  const merchantName = 'Mandello';
  const merchantId = 'mandello1';
  const merchantBgColor = '#FF5050';
  const merchantDID = 'did:cardstack:1pfsUmRoNRYTersTVPYgkhWE62b2cd7ce12b5fff';
  const merchantAddress = '0xaeFbA62A2B3e90FD131209CC94480E722704E1F8';
  const merchantRegistrationFee = 150;
  const merchantSafe: MerchantSafe = {
    type: 'merchant',
    address: merchantAddress,
    merchant: merchantName,
    infoDID: merchantDID,
    accumulatedSpendValue: 0,
    createdAt: Date.now(),
    tokens: [],
    owners: [layer2AccountAddress],
  };

  hooks.beforeEach(async function () {
    let layer2Service = this.owner.lookup('service:layer2-network')
      .strategy as Layer2TestWeb3Strategy;
    layer2Service.test__simulateAccountsChanged([layer2AccountAddress]);

    let depotAddress = '0xB236ca8DbAB0644ffCD32518eBF4924ba8666666';
    layer2Service.test__simulateAccountSafes(layer2AccountAddress, [
      {
        type: 'depot',
        address: depotAddress,
        tokens: [
          {
            balance: '250000000000000000000',
            tokenAddress: 'DAI_ADDRESS',
            token: {
              symbol: 'DAI',
              name: 'DAI',
              decimals: 18,
            },
          },
          {
            balance: '250000000000000000000',
            tokenAddress: 'CARD_ADDRESS',
            token: {
              symbol: 'CARD',
              name: 'CARD',
              decimals: 18,
            },
          },
        ],
        createdAt: Date.now() / 1000,
        owners: [layer2AccountAddress],
      },
      {
        type: 'prepaid-card',
        createdAt: Date.now() / 1000,

        address: '0x123400000000000000000000000000000000abcd',

        tokens: [],
        owners: [layer2AccountAddress],

        issuingToken: '<PASSWORD>',
        spendFaceValue: 2324,
        prepaidCardOwner: layer2AccountAddress,
        hasBeenUsed: false,
        issuer: layer2AccountAddress,
        reloadable: false,
        transferrable: false,
      },
    ]);
    layer2Service.authenticate();
    layer2Service.test__simulateHubAuthentication('abc123--def456--ghi789');

    workflowPersistenceService = this.owner.lookup(
      'service:workflow-persistence'
    );

    workflowPersistenceService.storage.clear();
  });

  test('Generates a flow uuid query parameter used as a persistence identifier', async function (this: Context, assert) {
    await visit('/card-pay/merchant-services');
    await click('[data-test-workflow-button="create-merchant"]');

    assert.equal(
      // @ts-ignore (complains object is possibly null)
      new URL('http://domain.test/' + currentURL()).searchParams.get('flow-id')
        .length,
      22
    );
  });

  module('Restoring from a previously saved state', function () {
    test('it restores an unfinished workflow', async function (this: Context, assert) {
      let state = buildState({
        meta: {
          completedCardNames: ['LAYER2_CONNECT', 'MERCHANT_CUSTOMIZATION'],<commit_msg>Update new test for hidden storage<commit_after>import { module, test } from 'qunit';
import { click, visit, currentURL, waitFor } from '@ember/test-helpers';
import { setupApplicationTest } from 'ember-qunit';
import { setupMirage } from 'ember-cli-mirage/test-support';
import { MirageTestContext } from 'ember-cli-mirage/test-support';
import Layer2TestWeb3Strategy from '@cardstack/web-client/utils/web3-strategies/test-layer2';
import WorkflowPersistence from '@cardstack/web-client/services/workflow-persistence';
import { MerchantSafe, PrepaidCardSafe } from '@cardstack/cardpay-sdk';
import { buildState } from '@cardstack/web-client/models/workflow/workflow-session';
import { setupHubAuthenticationToken } from '../helpers/setup';

interface Context extends MirageTestContext {}

module('Acceptance | create merchant persistence', function (hooks) {
  setupApplicationTest(hooks);
  setupMirage(hooks);
  setupHubAuthenticationToken(hooks);
  let workflowPersistenceService: WorkflowPersistence;
  let layer2AccountAddress = '0x182619c6Ea074C053eF3f1e1eF81Ec8De6Eb6E44';
  const prepaidCardAddress = '0x81c89274Dc7C9BAcE082d2ca00697d2d2857D2eE';
  const prepaidCard: PrepaidCardSafe = {
    type: 'prepaid-card',
    address: prepaidCardAddress,
    customizationDID: 'did:cardstack:1pkYh9uJHdfMJZt4mURGmhps96b157a2d744efd9',
    reloadable: false,
    spendFaceValue: 500,
    transferrable: true,
    createdAt: Date.now() / 1000,
    tokens: [],
    owners: [layer2AccountAddress],
    issuingToken: '<PASSWORD>',
    prepaidCardOwner: layer2AccountAddress,
    hasBeenUsed: false,
    issuer: layer2AccountAddress,
  };
  const merchantName = 'Mandello';
  const merchantId = 'mandello1';
  const merchantBgColor = '#FF5050';
  const merchantDID = 'did:cardstack:1pfsUmRoNRYTersTVPYgkhWE62b2cd7ce12b5fff';
  const merchantAddress = '0xaeFbA62A2B3e90FD131209CC94480E722704E1F8';
  const merchantRegistrationFee = 150;
  const merchantSafe: MerchantSafe = {
    type: 'merchant',
    address: merchantAddress,
    merchant: merchantName,
    infoDID: merchantDID,
    accumulatedSpendValue: 0,
    createdAt: Date.now(),
    tokens: [],
    owners: [layer2AccountAddress],
  };

  hooks.beforeEach(async function () {
    let layer2Service = this.owner.lookup('service:layer2-network')
      .strategy as Layer2TestWeb3Strategy;
    layer2Service.test__simulateAccountsChanged([layer2AccountAddress]);

    let depotAddress = '0xB236ca8DbAB0644ffCD32518eBF4924ba8666666';
    layer2Service.test__simulateAccountSafes(layer2AccountAddress, [
      {
        type: 'depot',
        address: depotAddress,
        tokens: [
          {
            balance: '250000000000000000000',
            tokenAddress: 'DAI_ADDRESS',
            token: {
              symbol: 'DAI',
              name: 'DAI',
              decimals: 18,
            },
          },
          {
            balance: '250000000000000000000',
            tokenAddress: 'CARD_ADDRESS',
            token: {
              symbol: 'CARD',
              name: 'CARD',
              decimals: 18,
            },
          },
        ],
        createdAt: Date.now() / 1000,
        owners: [layer2AccountAddress],
      },
      {
        type: 'prepaid-card',
        createdAt: Date.now() / 1000,

        address: '0x123400000000000000000000000000000000abcd',

        tokens: [],
        owners: [layer2AccountAddress],

        issuingToken: '0xTOKEN',
        spendFaceValue: 2324,
        prepaidCardOwner: layer2AccountAddress,
        hasBeenUsed: false,
        issuer: layer2AccountAddress,
        reloadable: false,
        transferrable: false,
      },
    ]);
    layer2Service.authenticate();
    layer2Service.test__simulateHubAuthentication('abc123--def456--ghi789');

    workflowPersistenceService = this.owner.lookup(
      'service:workflow-persistence'
    );

    workflowPersistenceService.clear();
  });

  test('Generates a flow uuid query parameter used as a persistence identifier', async function (this: Context, assert) {
    await visit('/card-pay/merchant-services');
    await click('[data-test-workflow-button="create-merchant"]');

    assert.equal(
      // @ts-ignore (complains object is possibly null)
      new URL('http://domain.test/' + currentURL()).searchParams.get('flow-id')
        .length,
      22
    );
  });

  module('Restoring from a previously saved state', function () {
    test('it restores an unfinished workflow', async function (this: Context, assert) {
      let state = buildState({
        meta: {
          completedCardNames: ['LAYER2_CONNECT', 'MERCHANT_CUSTOMIZATION'],
{'fraction_non_alphanumeric': 0.10945975265784336, 'fraction_numerical': 0.05803862009112606, 'mean_word_length': 3.3220815752461323, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 6, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6347809', 'n_tokens_mistral': 3141, 'n_tokens_neox': 2818, 'n_words': 530}