<commit_before>/**
 * @license
 * Copyright (c) 2018 Google Inc. All rights reserved.
 * This code may only be used under the BSD style license found at
 * http://polymer.github.io/LICENSE.txt
 * Code distributed by Google as part of this project is also
 * subject to an additional IP rights grant found at
 * http://polymer.github.io/PATENTS.txt
 */

import {Manifest} from '../../manifest.js';
import {assert} from '../chai-web.js';
import * as util from '../../testing/test-util.js';
import {Arc} from '../../arc.js';
import {MessageChannel} from '../../message-channel.js';
import {InnerPEC} from '../../inner-PEC.js';
import {StubLoader} from '../../testing/stub-loader.js';
import {getDevtoolsChannel} from '../../debug/devtools-channel-provider.js';
import {Random} from '../../random.js';
import {TestHelper} from '../../testing/test-helper.js';

describe('OuterPortAttachment', function() {
  it('produces dataflow messages on devtools channel', async () => {
    Random.seedForTests();
    let devtoolsChannelStub = getDevtoolsChannel({useStub: true});
    const testHelper = new TestHelper({
      loader: new StubLoader({
        'manifest': `
          schema Foo
            Text value
          particle P in 'p.js'
            inout Foo foo
          recipe
            use as foo
            P
              foo = foo`,
        'p.js': `defineParticle(({Particle}) => class P extends Particle {
          async setViews(views) {
            let foo = views.get('foo');
            foo.set(new foo.entityClass({value: 'FooBar'}));
          }
        });`
      })
    });
    await testHelper.loadManifest('manifest');
    const arc = testHelper.arc;
    arc.initDebug();

    const Foo = arc._context.findSchemaByName('Foo').entityClass();
    const fooHandle = await arc.createHandle(Foo.type, undefined, 'fooHandle');

    const recipe = arc._context.recipes[0];
    recipe.handles[0].mapToStorage(fooHandle);
    recipe.normalize();
    await arc.instantiate(recipe);

    let instantiateParticleCall = devtoolsChannelStub.messages.find(m =>
        m.messageType === 'InstantiateParticle').messageBody;
    // IDs are stable thanks to Random.seedForTests().
    assert.deepEqual(instantiateParticleCall, {
      arcId: '!158405822139616:demo',
      speculative: false,
      id: '!158405822139616:demo:particle1',
      name: 'P',
      connections: {
        foo: {
          direction: 'inout',
          id: 'fooHandle',
          storageKey: 'in-memory://!158405822139616:demo^^in-memory-0',
          type: 'Foo'
        },
      },
      implFile: 'p.js'
    });

    await util.assertSingletonWillChangeTo(fooHandle, Foo, 'FooBar');
    let dateflowSetCall = devtoolsChannelStub.messages.find(m =>
        m.messageType === 'dataflow' &&
        m.messageBody.operation === 'set').messageBody;

    assert.approximately(dateflowSetCall.timestamp, Date.now(), 2000);
    delete dateflowSetCall.timestamp; // This bit we don't want to assert on.

    assert.deepEqual(dateflowSetCall, {
      arcId: '!158405822139616:demo',
      speculative: false,
      operation: 'set',
      particle: {
        id: '!158405822139616:demo:particle1',
        name: 'P'
      },
      handle: {
        id: 'fooHandle',
        storageKey: 'in-memory://!158405822139616:demo^^in-memory-0',
        type: 'Foo'
      },
      data: '{"id":"!158405822139616:demo:0:inner:0","rawData":{"value":"FooBar"}}',
    });
  });
});<commit_msg>Fixes OuterPortAttachment test (#1391)<commit_after>/**
 * @license
 * Copyright (c) 2018 Google Inc. All rights reserved.
 * This code may only be used under the BSD style license found at
 * http://polymer.github.io/LICENSE.txt
 * Code distributed by Google as part of this project is also
 * subject to an additional IP rights grant found at
 * http://polymer.github.io/PATENTS.txt
 */

import {Manifest} from '../../manifest.js';
import {assert} from '../chai-web.js';
import * as util from '../../testing/test-util.js';
import {Arc} from '../../arc.js';
import {MessageChannel} from '../../message-channel.js';
import {InnerPEC} from '../../inner-PEC.js';
import {StubLoader} from '../../testing/stub-loader.js';
import {getDevtoolsChannel} from '../../debug/devtools-channel-provider.js';
import {Random} from '../../random.js';
import {TestHelper} from '../../testing/test-helper.js';

describe('OuterPortAttachment', function() {
  it('produces dataflow messages on devtools channel', async () => {
    Random.seedForTests();
    let devtoolsChannelStub = getDevtoolsChannel({useStub: true});
    const testHelper = new TestHelper({
      loader: new StubLoader({
        'manifest': `
          schema Foo
            Text value
          particle P in 'p.js'
            inout Foo foo
          recipe
            use as foo
            P
              foo = foo`,
        'p.js': `defineParticle(({Particle}) => class P extends Particle {
          async setHandles(handles) {
            let foo = handles.get('foo');
            foo.set(new foo.entityClass({value: 'FooBar'}));
          }
        });`
      })
    });
    await testHelper.loadManifest('manifest');
    const arc = testHelper.arc;
    arc.initDebug();

    const Foo = arc._context.findSchemaByName('Foo').entityClass();
    const fooStore = await arc.createStore(Foo.type, undefined, 'fooStore');

    const recipe = arc._context.recipes[0];
    recipe.handles[0].mapToStorage(fooStore);
    recipe.normalize();
    await arc.instantiate(recipe);

    let instantiateParticleCall = devtoolsChannelStub.messages.find(m =>
        m.messageType === 'InstantiateParticle').messageBody;
    // IDs are stable thanks to Random.seedForTests().
    assert.deepEqual(instantiateParticleCall, {
      arcId: '!158405822139616:demo',
      speculative: false,
      id: '!158405822139616:demo:particle1',
      name: 'P',
      connections: {
        foo: {
          direction: 'inout',
          id: 'fooStore',
          storageKey: 'in-memory://!158405822139616:demo^^in-memory-0',
          type: 'Foo'
        },
      },
      implFile: 'p.js'
    });

    await util.assertSingletonWillChangeTo(fooStore, Foo, 'FooBar');
    let dateflowSetCall = devtoolsChannelStub.messages.find(m =>
        m.messageType === 'dataflow' &&
        m.messageBody.operation === 'set').messageBody;

    assert.approximately(dateflowSetCall.timestamp, Date.now(), 2000);
    delete dateflowSetCall.timestamp; // This bit we don't want to assert on.

    assert.deepEqual(dateflowSetCall, {
      arcId: '!158405822139616:demo',
      speculative: false,
      operation: 'set',
      particle: {
        id: '!158405822139616:demo:particle1',
        name: 'P'
      },
      handle: {
        id: 'fooStore',
        storageKey: 'in-memory://!158405822139616:demo^^in-memory-0',
        type: 'Foo'
      },
      data: '{"id":"!158405822139616:demo:0:inner:0","rawData":{"value":"FooBar"}}',
    });
  });
});
{'fraction_non_alphanumeric': 0.15294627383015597, 'fraction_numerical': 0.03552859618717504, 'mean_word_length': 2.6953041622198506, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '3428747', 'n_tokens_mistral': 2266, 'n_tokens_neox': 2118, 'n_words': 481}