|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +import assert from "assert"; |
| 6 | +import { Context } from "../../../../src/core/context"; |
| 7 | +import { ErrorType } from "../../../../src/core/error/error_type"; |
| 8 | + |
| 9 | +import Glean from "../../../../src/core/glean"; |
| 10 | +import { Lifetime } from "../../../../src/core/metrics/lifetime"; |
| 11 | +import TextMetricType, { TEXT_MAX_LENGTH } from "../../../../src/core/metrics/types/text"; |
| 12 | + |
| 13 | +describe("TextMetric", function() { |
| 14 | + const testAppId = `gleanjs.test.${this.title}`; |
| 15 | + |
| 16 | + beforeEach(async function() { |
| 17 | + await Glean.testResetGlean(testAppId); |
| 18 | + }); |
| 19 | + |
| 20 | + it("attempting to get the value of a metric that hasn't been recorded doesn't error", async function() { |
| 21 | + const metric = new TextMetricType({ |
| 22 | + category: "aCategory", |
| 23 | + name: "aTextMetric", |
| 24 | + sendInPings: ["aPing", "twoPing", "threePing"], |
| 25 | + lifetime: Lifetime.Ping, |
| 26 | + disabled: false |
| 27 | + }); |
| 28 | + |
| 29 | + assert.strictEqual(await metric.testGetValue("aPing"), undefined); |
| 30 | + }); |
| 31 | + |
| 32 | + it("attempting to set when glean upload is disabled is a no-op", async function() { |
| 33 | + Glean.setUploadEnabled(false); |
| 34 | + |
| 35 | + const metric = new TextMetricType({ |
| 36 | + category: "aCategory", |
| 37 | + name: "aTextMetric", |
| 38 | + sendInPings: ["aPing", "twoPing", "threePing"], |
| 39 | + lifetime: Lifetime.Ping, |
| 40 | + disabled: false |
| 41 | + }); |
| 42 | + |
| 43 | + metric.set("some value"); |
| 44 | + assert.strictEqual(await metric.testGetValue("aPing"), undefined); |
| 45 | + }); |
| 46 | + |
| 47 | + it("ping payload is correct", async function() { |
| 48 | + const metric = new TextMetricType({ |
| 49 | + category: "aCategory", |
| 50 | + name: "aTextMetric", |
| 51 | + sendInPings: ["aPing"], |
| 52 | + lifetime: Lifetime.Ping, |
| 53 | + disabled: false |
| 54 | + }); |
| 55 | + |
| 56 | + const validValues = [ |
| 57 | + "some value", |
| 58 | + "<html><head><title>Website</title></head><body><h1>Text</h1></body>", |
| 59 | + "some longer text\nwith newlines\nand also some quotes: \"once upon a time ...\"", |
| 60 | + ]; |
| 61 | + |
| 62 | + for (const value of validValues) { |
| 63 | + metric.set(value); |
| 64 | + assert.strictEqual(await metric.testGetValue("aPing"), value); |
| 65 | + |
| 66 | + const snapshot = await Context.metricsDatabase.getPingMetrics("aPing", true); |
| 67 | + assert.deepStrictEqual(snapshot, { |
| 68 | + "text": { |
| 69 | + "aCategory.aTextMetric": value |
| 70 | + } |
| 71 | + }); |
| 72 | + } |
| 73 | + }); |
| 74 | + |
| 75 | + it("set properly sets the value in all pings", async function() { |
| 76 | + const metric = new TextMetricType({ |
| 77 | + category: "aCategory", |
| 78 | + name: "aTextMetric", |
| 79 | + sendInPings: ["aPing", "twoPing", "threePing"], |
| 80 | + lifetime: Lifetime.Ping, |
| 81 | + disabled: false |
| 82 | + }); |
| 83 | + |
| 84 | + metric.set("some value"); |
| 85 | + assert.strictEqual(await metric.testGetValue("aPing"), "some value"); |
| 86 | + assert.strictEqual(await metric.testGetValue("twoPing"), "some value"); |
| 87 | + assert.strictEqual(await metric.testGetValue("threePing"), "some value"); |
| 88 | + }); |
| 89 | + |
| 90 | + it("truncates when text exceeds maximum length and records errors", async function () { |
| 91 | + const metric = new TextMetricType({ |
| 92 | + category: "aCategory", |
| 93 | + name: "aTextMetric", |
| 94 | + sendInPings: ["aPing"], |
| 95 | + lifetime: Lifetime.Ping, |
| 96 | + disabled: false |
| 97 | + }); |
| 98 | + |
| 99 | + const testText = `some value ${"a".repeat(TEXT_MAX_LENGTH)}`; |
| 100 | + metric.set(testText); |
| 101 | + const truncated = testText.substr(0, TEXT_MAX_LENGTH); |
| 102 | + |
| 103 | + assert.strictEqual(await metric.testGetValue("aPing"), truncated); |
| 104 | + assert.strictEqual( |
| 105 | + await metric.testGetNumRecordedErrors(ErrorType.InvalidOverflow), 1 |
| 106 | + ); |
| 107 | + }); |
| 108 | +}); |
0 commit comments