<commit_before>/**
 * @license
 * Copyright Google LLC All Rights Reserved.
 *
 * Use of this source code is governed by an MIT-style license that can be
 * found in the LICENSE file at https://angular.io/license
 */

import {NgswCommChannel} from '@angular/service-worker/src/low_level';
import {SwPush} from '@angular/service-worker/src/push';
import {SwUpdate} from '@angular/service-worker/src/update';
import {MockServiceWorkerContainer, MockServiceWorkerRegistration} from '@angular/service-worker/testing/mock';
import {CacheDatabase} from '@angular/service-worker/worker/src/db-cache';
import {Driver} from '@angular/service-worker/worker/src/driver';
import {Manifest} from '@angular/service-worker/worker/src/manifest';
import {MockRequest} from '@angular/service-worker/worker/testing/fetch';
import {MockFileSystemBuilder, MockServerStateBuilder, tmpHashTableForFs} from '@angular/service-worker/worker/testing/mock';
import {SwTestHarness, SwTestHarnessBuilder} from '@angular/service-worker/worker/testing/scope';
import {Observable} from 'rxjs';
import {take} from 'rxjs/operators';

(function() {
// Skip environments that don't support the minimum APIs needed to run the SW tests.
if (!SwTestHarness.envIsSupported()) {
  return;
}

const dist = new MockFileSystemBuilder().addFile('/only.txt', 'this is only').build();

const distUpdate = new MockFileSystemBuilder().addFile('/only.txt', 'this is only v2').build();

function obsToSinglePromise<T>(obs: Observable<T>): Promise<T> {
  return obs.pipe(take(1)).toPromise();
}

const manifest: Manifest = {
  configVersion: 1,
  timestamp: 1234567890123,
  appData: {version: '1'},
  index: '/only.txt',
  assetGroups: [{
    name: 'assets',
    installMode: 'prefetch',
    updateMode: 'prefetch',
    urls: ['/only.txt'],
    patterns: [],
    cacheQueryOptions: {ignoreVary: true},
  }],
  navigationUrls: [],
  navigationRequestStrategy: 'performance',
  hashTable: tmpHashTableForFs(dist),
};

const manifestUpdate: Manifest = {
  configVersion: 1,
  timestamp: 1234567890123,
  appData: {version: '2'},
  index: '/only.txt',
  assetGroups: [{
    name: 'assets',
    installMode: 'prefetch',
    updateMode: 'prefetch',
    urls: ['/only.txt'],
    patterns: [],
    cacheQueryOptions: {ignoreVary: true},
  }],
  navigationUrls: [],
  navigationRequestStrategy: 'performance',
  hashTable: tmpHashTableForFs(distUpdate),
};

const server = new MockServerStateBuilder().withStaticFiles(dist).withManifest(manifest).build();

const serverUpdate =
    new MockServerStateBuilder().withStaticFiles(distUpdate).withManifest(manifestUpdate).build();


describe('ngsw + companion lib', () => {
  let mock: MockServiceWorkerContainer;
  let comm: NgswCommChannel;
  let reg: MockServiceWorkerRegistration;
  let scope: SwTestHarness;
  let driver: Driver;

  beforeEach(async () => {
    // Fire up the client.
    mock = new MockServiceWorkerContainer();
    comm = new NgswCommChannel(mock as any);
    scope = new SwTestHarnessBuilder().withServerState(server).build();
    driver = new Driver(scope, scope, new CacheDatabase(scope));

    scope.clients.add('default');
    scope.clients.getMock('default')!.queue.subscribe(msg => {
      mock.sendMessage(msg);
    });

    mock.messages.subscribe(msg => {
      scope.handleMessage(msg, 'default');
    });
    mock.notificationClicks.subscribe((msg: Object) => {
      scope.handleMessage(msg, 'default');
    });

    mock.setupSw();
    reg = mock.mockRegistration!;

    await Promise.all(scope.handleFetch(new MockRequest('/only.txt'), 'default'));
    await driver.initialized;
  });

  it('communicates back and forth via update check', async () => {
    const update = new SwUpdate(comm);
    await update.checkForUpdate();
  });

  it('detects an actual update', async () => {
    const update = new SwUpdate(comm);
    scope.updateServerState(serverUpdate);

    const gotUpdateNotice = (async () => {
      const notice = await obsToSinglePromise(update.available);
    })();

    await update.checkForUpdate();
    await gotUpdateNotice;
  });<commit_msg>refactor(service-worker): remove unused variables and imports from tests (#42736)<commit_after>/**
 * @license
 * Copyright Google LLC All Rights Reserved.
 *
 * Use of this source code is governed by an MIT-style license that can be
 * found in the LICENSE file at https://angular.io/license
 */

import {NgswCommChannel} from '@angular/service-worker/src/low_level';
import {SwPush} from '@angular/service-worker/src/push';
import {SwUpdate} from '@angular/service-worker/src/update';
import {MockServiceWorkerContainer} from '@angular/service-worker/testing/mock';
import {CacheDatabase} from '@angular/service-worker/worker/src/db-cache';
import {Driver} from '@angular/service-worker/worker/src/driver';
import {Manifest} from '@angular/service-worker/worker/src/manifest';
import {MockRequest} from '@angular/service-worker/worker/testing/fetch';
import {MockFileSystemBuilder, MockServerStateBuilder, tmpHashTableForFs} from '@angular/service-worker/worker/testing/mock';
import {SwTestHarness, SwTestHarnessBuilder} from '@angular/service-worker/worker/testing/scope';
import {Observable} from 'rxjs';
import {take} from 'rxjs/operators';

(function() {
// Skip environments that don't support the minimum APIs needed to run the SW tests.
if (!SwTestHarness.envIsSupported()) {
  return;
}

const dist = new MockFileSystemBuilder().addFile('/only.txt', 'this is only').build();

const distUpdate = new MockFileSystemBuilder().addFile('/only.txt', 'this is only v2').build();

function obsToSinglePromise<T>(obs: Observable<T>): Promise<T> {
  return obs.pipe(take(1)).toPromise();
}

const manifest: Manifest = {
  configVersion: 1,
  timestamp: 1234567890123,
  appData: {version: '1'},
  index: '/only.txt',
  assetGroups: [{
    name: 'assets',
    installMode: 'prefetch',
    updateMode: 'prefetch',
    urls: ['/only.txt'],
    patterns: [],
    cacheQueryOptions: {ignoreVary: true},
  }],
  navigationUrls: [],
  navigationRequestStrategy: 'performance',
  hashTable: tmpHashTableForFs(dist),
};

const manifestUpdate: Manifest = {
  configVersion: 1,
  timestamp: 1234567890123,
  appData: {version: '2'},
  index: '/only.txt',
  assetGroups: [{
    name: 'assets',
    installMode: 'prefetch',
    updateMode: 'prefetch',
    urls: ['/only.txt'],
    patterns: [],
    cacheQueryOptions: {ignoreVary: true},
  }],
  navigationUrls: [],
  navigationRequestStrategy: 'performance',
  hashTable: tmpHashTableForFs(distUpdate),
};

const server = new MockServerStateBuilder().withStaticFiles(dist).withManifest(manifest).build();

const serverUpdate =
    new MockServerStateBuilder().withStaticFiles(distUpdate).withManifest(manifestUpdate).build();


describe('ngsw + companion lib', () => {
  let mock: MockServiceWorkerContainer;
  let comm: NgswCommChannel;
  let scope: SwTestHarness;
  let driver: Driver;

  beforeEach(async () => {
    // Fire up the client.
    mock = new MockServiceWorkerContainer();
    comm = new NgswCommChannel(mock as any);
    scope = new SwTestHarnessBuilder().withServerState(server).build();
    driver = new Driver(scope, scope, new CacheDatabase(scope));

    scope.clients.add('default');
    scope.clients.getMock('default')!.queue.subscribe(msg => {
      mock.sendMessage(msg);
    });

    mock.messages.subscribe(msg => {
      scope.handleMessage(msg, 'default');
    });
    mock.notificationClicks.subscribe((msg: Object) => {
      scope.handleMessage(msg, 'default');
    });

    mock.setupSw();

    await Promise.all(scope.handleFetch(new MockRequest('/only.txt'), 'default'));
    await driver.initialized;
  });

  it('communicates back and forth via update check', async () => {
    const update = new SwUpdate(comm);
    await update.checkForUpdate();
  });

  it('detects an actual update', async () => {
    const update = new SwUpdate(comm);
    scope.updateServerState(serverUpdate);

    const gotUpdateNotice = (async () => await obsToSinglePromise(update.available))();

    await update.checkForUpdate();
    await gotUpdateNotice;
  });
{'fraction_non_alphanumeric': 0.14439096630877452, 'fraction_numerical': 0.008515364679748241, 'mean_word_length': 4.679046951646812, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 25, 'https://': 2, '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': '6606676', 'n_tokens_mistral': 2466, 'n_tokens_neox': 2388, 'n_words': 564}