{"size": 4855, "ext": "js", "max_stars_repo_path": "ui/file_manager/file_manager/foreground/js/metadata/metadata_cache_item_unittest.m.js", "max_stars_repo_name": "zealoussnow/chromium", "max_stars_repo_stars_event_min_datetime": "2015-01-01T01:57:10.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-31T23:33:32.000Z", "max_issues_repo_path": "ui/file_manager/file_manager/foreground/js/metadata/metadata_cache_item_unittest.m.js", "max_issues_repo_name": "zealoussnow/chromium", "max_issues_repo_issues_event_min_datetime": "2015-10-21T13:02:42.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-14T07:50:50.000Z", "max_forks_repo_path": "ui/file_manager/file_manager/foreground/js/metadata/metadata_cache_item_unittest.m.js", "max_forks_repo_name": "zealoussnow/chromium", "max_forks_repo_forks_event_min_datetime": "2015-01-02T11:32:21.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-31T16:35:46.000Z"}
{"max_stars_count": 14668, "max_issues_count": 86, "max_forks_count": 5941, "avg_line_length": 34.4326241135, "max_line_length": 97, "alphanum_fraction": 0.7604531411}
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import {assertEquals, assertFalse, assertThrows, assertTrue} from 'chrome://test/chai_assert.js';
import {MetadataCacheItem} from './metadata_cache_item.js';
import {MetadataItem} from './metadata_item.js';

/**
 * @type {!MetadataItem}
 */
const metadataA = new MetadataItem();
metadataA.contentMimeType = 'value';


export function testMetadataCacheItemBasic() {
  const item = new MetadataCacheItem();
  const loadRequested = item.createRequests(['contentMimeType']);
  assertEquals(1, loadRequested.length);
  assertEquals('contentMimeType', loadRequested[0]);

  item.startRequests(1, loadRequested);
  assertTrue(item.storeProperties(1, metadataA));

  const result = item.get(['contentMimeType']);
  assertEquals('value', result.contentMimeType);
}

export function testMetadataCacheItemAvoidDoubleLoad() {
  const item = new MetadataCacheItem();
  item.startRequests(1, ['contentMimeType']);
  const loadRequested = item.createRequests(['contentMimeType']);
  assertEquals(0, loadRequested.length);

  item.startRequests(2, loadRequested);
  assertTrue(item.storeProperties(1, metadataA));

  const result = item.get(['contentMimeType']);
  assertEquals('value', result.contentMimeType);
}

export function testMetadataCacheItemInvalidate() {
  const item = new MetadataCacheItem();
  item.startRequests(1, item.createRequests(['contentMimeType']));
  item.invalidate(2);
  assertFalse(item.storeProperties(1, metadataA));

  const loadRequested = item.createRequests(['contentMimeType']);
  assertEquals(1, loadRequested.length);
}

export function testMetadataCacheItemStoreInReverseOrder() {
  const item = new MetadataCacheItem();
  item.startRequests(1, item.createRequests(['contentMimeType']));
  item.startRequests(2, item.createRequests(['contentMimeType']));

  const metadataB = new MetadataItem();
  metadataB.contentMimeType = 'value2';

  assertTrue(item.storeProperties(2, metadataB));
  assertFalse(item.storeProperties(1, metadataA));

  const result = item.get(['contentMimeType']);
  assertEquals('value2', result.contentMimeType);
}

export function testMetadataCacheItemClone() {
  const itemA = new MetadataCacheItem();
  itemA.startRequests(1, itemA.createRequests(['contentMimeType']));
  const itemB = itemA.clone();
  itemA.storeProperties(1, metadataA);
  assertFalse(itemB.hasFreshCache(['contentMimeType']));

  itemB.storeProperties(1, metadataA);
  assertTrue(itemB.hasFreshCache(['contentMimeType']));

  itemA.invalidate(2);
  assertTrue(itemB.hasFreshCache(['contentMimeType']));
}

export function testMetadataCacheItemHasFreshCache() {
  const item = new MetadataCacheItem();
  assertFalse(item.hasFreshCache(['contentMimeType', 'externalFileUrl']));

  item.startRequests(
      1, item.createRequests(['contentMimeType', 'externalFileUrl']));

  const metadata = new MetadataItem();
  metadata.contentMimeType = 'mime';
  metadata.externalFileUrl = 'url';

  item.storeProperties(1, metadata);
  assertTrue(item.hasFreshCache(['contentMimeType', 'externalFileUrl']));

  item.invalidate(2);
  assertFalse(item.hasFreshCache(['contentMimeType', 'externalFileUrl']));

  item.startRequests(1, item.createRequests(['contentMimeType']));
  item.storeProperties(1, metadataA);
  assertFalse(item.hasFreshCache(['contentMimeType', 'externalFileUrl']));
  assertTrue(item.hasFreshCache(['contentMimeType']));
}

export function testMetadataCacheItemShouldNotUpdateBeforeInvalidation() {
  const item = new MetadataCacheItem();
  item.startRequests(1, item.createRequests(['contentMimeType']));
  item.storeProperties(1, metadataA);

  const metadataB = new MetadataItem();
  metadataB.contentMimeType = 'value2';

  item.storeProperties(2, metadataB);
  assertEquals('value', item.get(['contentMimeType']).contentMimeType);
}

export function testMetadataCacheItemError() {
  const item = new MetadataCacheItem();
  item.startRequests(1, item.createRequests(['contentThumbnailUrl']));

  const metadataWithError = new MetadataItem();
  metadataWithError.contentThumbnailUrlError = new Error('Error');

  item.storeProperties(1, metadataWithError);
  const property = item.get(['contentThumbnailUrl']);
  assertEquals(undefined, property.contentThumbnailUrl);
  assertEquals('Error', property.contentThumbnailUrlError.message);
}

export function testMetadataCacheItemErrorShouldNotFetchedDirectly() {
  const item = new MetadataCacheItem();
  item.startRequests(1, item.createRequests(['contentThumbnailUrl']));

  const metadataWithError = new MetadataItem();
  metadataWithError.contentThumbnailUrlError = new Error('Error');

  item.storeProperties(1, metadataWithError);
  assertThrows(() => {
    item.get(['contentThumbnailUrlError']);
  });
}
