|
| 1 | +// Copyright 2025 The Chromium Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import * as Protocol from '../../../generated/protocol.js'; |
| 6 | +import {describeWithEnvironment} from '../../../testing/EnvironmentHelpers.js'; |
| 7 | +import {getFirstOrError, getInsightOrError, processTrace} from '../../../testing/InsightHelpers.js'; |
| 8 | +import type * as Trace from '../../trace/trace.js'; |
| 9 | + |
| 10 | +import * as UseCache from './UseCache.js'; |
| 11 | + |
| 12 | +describeWithEnvironment('UseCache', function() { |
| 13 | + describe('isCacheable', () => { |
| 14 | + it('should return true for cacheable requests', () => { |
| 15 | + const cacheableRequest = { |
| 16 | + args: { |
| 17 | + data: { |
| 18 | + protocol: 'https', |
| 19 | + resourceType: Protocol.Network.ResourceType.Script, |
| 20 | + statusCode: 200, |
| 21 | + }, |
| 22 | + }, |
| 23 | + } as unknown as Trace.Types.Events.SyntheticNetworkRequest; |
| 24 | + assert.isTrue(UseCache.isCacheable(cacheableRequest)); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should return false for non-network scheme: file', () => { |
| 28 | + const nonNetworkRequest = { |
| 29 | + args: { |
| 30 | + data: { |
| 31 | + protocol: 'file', |
| 32 | + resourceType: Protocol.Network.ResourceType.Script, |
| 33 | + statusCode: 200, |
| 34 | + }, |
| 35 | + }, |
| 36 | + } as unknown as Trace.Types.Events.SyntheticNetworkRequest; |
| 37 | + assert.isFalse(UseCache.isCacheable(nonNetworkRequest)); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should return false for non-cacheable status codes', () => { |
| 41 | + const nonCacheableRequest = { |
| 42 | + args: { |
| 43 | + data: { |
| 44 | + protocol: 'https', |
| 45 | + resourceType: Protocol.Network.ResourceType.Script, |
| 46 | + statusCode: 404, |
| 47 | + }, |
| 48 | + }, |
| 49 | + } as unknown as Trace.Types.Events.SyntheticNetworkRequest; |
| 50 | + assert.isFalse(UseCache.isCacheable(nonCacheableRequest)); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should return false for non-static resource types', () => { |
| 54 | + const nonCacheableRequest = { |
| 55 | + args: { |
| 56 | + data: { |
| 57 | + protocol: 'https', |
| 58 | + resourceType: Protocol.Network.ResourceType.XHR, |
| 59 | + statusCode: 200, |
| 60 | + }, |
| 61 | + }, |
| 62 | + } as unknown as Trace.Types.Events.SyntheticNetworkRequest; |
| 63 | + assert.isFalse(UseCache.isCacheable(nonCacheableRequest)); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + describe('computeCacheLifetimeInSeconds', () => { |
| 68 | + it('should return max-age if defined', () => { |
| 69 | + const headers = [{name: 'cache-control', value: 'max-age=3600'}]; |
| 70 | + const cacheControl = { |
| 71 | + 'max-age': 3600, |
| 72 | + }; |
| 73 | + assert.strictEqual(UseCache.computeCacheLifetimeInSeconds(headers, cacheControl), 3600); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should return expires header if defined and max-age is not defined', () => { |
| 77 | + const now = Date.now(); |
| 78 | + const future = new Date(now + 5000).toUTCString(); |
| 79 | + const headers = [{name: 'expires', value: future}]; |
| 80 | + const cacheControl = null; |
| 81 | + assert.strictEqual(UseCache.computeCacheLifetimeInSeconds(headers, cacheControl), 5); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should handle negative max-age', () => { |
| 85 | + const past = new Date(Date.now() - 5000).toUTCString(); |
| 86 | + const headers = [{name: 'expires', value: past}]; |
| 87 | + const cacheControl = null; |
| 88 | + assert.strictEqual(UseCache.computeCacheLifetimeInSeconds(headers, cacheControl), -5); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should return null if neither max-age nor expires is defined', () => { |
| 92 | + const headers: Array<{name: string, value: string}> = []; |
| 93 | + const cacheControl = null; |
| 94 | + assert.isNull(UseCache.computeCacheLifetimeInSeconds(headers, cacheControl)); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should return 0 if expires is invalid', () => { |
| 98 | + const headers = [{name: 'expires', value: 'invalid date'}]; |
| 99 | + const cacheControl = null; |
| 100 | + assert.strictEqual(UseCache.computeCacheLifetimeInSeconds(headers, cacheControl), 0); |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + describe('cachingDisabled', () => { |
| 105 | + it('should return true if cache-control contains no-cache', () => { |
| 106 | + const headers = new Map([['cache-control', 'no-cache']]); |
| 107 | + const parsedCacheControl = { |
| 108 | + 'no-cache': true, |
| 109 | + }; |
| 110 | + assert.isTrue(UseCache.cachingDisabled(headers, parsedCacheControl)); |
| 111 | + }); |
| 112 | + |
| 113 | + it('should return true if cache-control contains no-store', () => { |
| 114 | + const headers = new Map([['cache-control', 'no-store']]); |
| 115 | + const parsedCacheControl = { |
| 116 | + 'no-store': true, |
| 117 | + }; |
| 118 | + assert.isTrue(UseCache.cachingDisabled(headers, parsedCacheControl)); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should return true if cache-control contains must-revalidate', () => { |
| 122 | + const headers = new Map([['cache-control', 'must-revalidate']]); |
| 123 | + const parsedCacheControl = { |
| 124 | + 'must-revalidate': true, |
| 125 | + }; |
| 126 | + assert.isTrue(UseCache.cachingDisabled(headers, parsedCacheControl)); |
| 127 | + }); |
| 128 | + |
| 129 | + it('should return true if cache-control contains private', () => { |
| 130 | + const headers = new Map([['cache-control', 'private']]); |
| 131 | + |
| 132 | + const parsedCacheControl = { |
| 133 | + private: true, |
| 134 | + }; |
| 135 | + assert.isTrue(UseCache.cachingDisabled(headers, parsedCacheControl)); |
| 136 | + }); |
| 137 | + |
| 138 | + it('should return true if pragma contains no-cache and no cache-control', () => { |
| 139 | + const headers = new Map([['pragma', 'no-cache']]); |
| 140 | + const parsedCacheControl = null; |
| 141 | + assert.isTrue(UseCache.cachingDisabled(headers, parsedCacheControl)); |
| 142 | + }); |
| 143 | + |
| 144 | + it('should return false if no disabling headers are present', () => { |
| 145 | + const headers = new Map([['cache-control', 'max-age=3600']]); |
| 146 | + const parsedCacheControl = { |
| 147 | + 'max-age': 3600, |
| 148 | + }; |
| 149 | + assert.isFalse(UseCache.cachingDisabled(headers, parsedCacheControl)); |
| 150 | + }); |
| 151 | + |
| 152 | + it('should return false if pragma contains no-cache but cache-control is present', () => { |
| 153 | + const headers = new Map([['cache-control', 'max-age=3600'], ['pragma', 'no-cache']]); |
| 154 | + const parsedCacheControl = { |
| 155 | + 'max-age': 3600, |
| 156 | + }; |
| 157 | + assert.isFalse(UseCache.cachingDisabled(headers, parsedCacheControl)); |
| 158 | + }); |
| 159 | + }); |
| 160 | + describe('getHeaders', () => { |
| 161 | + it('should handle multiple headers with the same name', () => { |
| 162 | + const responseHeaders = [ |
| 163 | + {name: 'cache-control', value: 'max-age=3600'}, |
| 164 | + {name: 'cache-control', value: 'public'}, |
| 165 | + {name: 'content-type', value: 'text/css'}, |
| 166 | + ]; |
| 167 | + const headers = UseCache.getCombinedHeaders(responseHeaders); |
| 168 | + assert.strictEqual(headers.get('cache-control'), 'max-age=3600, public'); |
| 169 | + assert.strictEqual(headers.get('content-type'), 'text/css'); |
| 170 | + }); |
| 171 | + }); |
| 172 | + |
| 173 | + describe('generateInsight', () => { |
| 174 | + it('generateInsight - no cacheable requests', async () => { |
| 175 | + const {data, insights} = await processTrace(this, 'load-simple.json.gz'); |
| 176 | + const insight = |
| 177 | + getInsightOrError('UseCache', insights, getFirstOrError(data.Meta.navigationsByNavigationId.values())); |
| 178 | + |
| 179 | + const relatedEvents = insight.relatedEvents as Trace.Types.Events.SyntheticNetworkRequest[]; |
| 180 | + assert.strictEqual(insight.insightKey, 'UseCache'); |
| 181 | + assert.strictEqual(insight.state, 'pass'); |
| 182 | + assert.deepEqual(insight.strings, UseCache.UIStrings); |
| 183 | + assert.strictEqual(insight.requests?.length, 0); |
| 184 | + assert.deepEqual(insight.totalWastedBytes, 0); |
| 185 | + assert.strictEqual(relatedEvents?.length, 0); |
| 186 | + }); |
| 187 | + |
| 188 | + it('generateInsight - cacheable requests', async () => { |
| 189 | + /** |
| 190 | + * Contains 4 network requests: |
| 191 | + * (1) page html request: not cacheable, Document resource type |
| 192 | + * (2) stylesheet Google Fonts css2: caching is disabled |
| 193 | + * (3) stylesheet app.css: ~ should recommend caching ~ |
| 194 | + * (4) via.placeholder img jpg: maxAgeInHours 8766, has high enough cache probability |
| 195 | + * (5) via.placeholder img jpeg: maxAgeInHours 8766, has high enough cache probability |
| 196 | + */ |
| 197 | + const {data, insights} = await processTrace(this, 'lcp-images.json.gz'); |
| 198 | + const insight = |
| 199 | + getInsightOrError('UseCache', insights, getFirstOrError(data.Meta.navigationsByNavigationId.values())); |
| 200 | + |
| 201 | + const relatedEvents = insight.relatedEvents as Trace.Types.Events.SyntheticNetworkRequest[] ?? []; |
| 202 | + assert.strictEqual(insight.insightKey, 'UseCache'); |
| 203 | + assert.strictEqual(insight.state, 'fail'); |
| 204 | + assert.deepEqual(insight.strings, UseCache.UIStrings); |
| 205 | + |
| 206 | + const gotCacheable = insight.requests; |
| 207 | + // Should have the 1 cacheable request. |
| 208 | + assert.deepEqual(gotCacheable?.length, 1); |
| 209 | + assert.deepEqual( |
| 210 | + gotCacheable[0].request.args.data.url, |
| 211 | + 'https://chromedevtools.github.io/performance-stories/lcp-large-image/app.css'); |
| 212 | + // 10min |
| 213 | + assert.deepEqual(gotCacheable[0].ttl, 600); |
| 214 | + // request's transfer size is 0. |
| 215 | + assert.deepEqual(gotCacheable[0].wastedBytes, 0); |
| 216 | + |
| 217 | + assert.deepEqual(relatedEvents?.length, 1); |
| 218 | + assert.deepEqual(insight.totalWastedBytes, 0); |
| 219 | + }); |
| 220 | + }); |
| 221 | +}); |
0 commit comments