TargetingDocs.test.js (3066B)
1 import { ASRouterTargeting } from "modules/ASRouterTargeting.sys.mjs"; 2 import docs from "docs/targeting-attributes.md"; 3 4 // The following targeting parameters are either deprecated or should not be included in the docs for some reason. 5 const SKIP_DOCS = []; 6 // These are extra message context attributes via ASRouter.sys.mjs 7 const MESSAGE_CONTEXT_ATTRIBUTES = ["previousSessionEnd"]; 8 9 function getHeadingsFromDocs() { 10 const re = /### `(\w+)`/g; 11 const found = []; 12 let match = 1; 13 while (match) { 14 match = re.exec(docs); 15 if (match) { 16 found.push(match[1]); 17 } 18 } 19 return found; 20 } 21 22 function getTOCFromDocs() { 23 const re = /## Available attributes\n+([^]+)\n+## Detailed usage/; 24 const sectionMatch = docs.match(re); 25 if (!sectionMatch) { 26 return []; 27 } 28 const [, listText] = sectionMatch; 29 const re2 = /\[(\w+)\]/g; 30 const found = []; 31 let match = 1; 32 while (match) { 33 match = re2.exec(listText); 34 if (match) { 35 found.push(match[1]); 36 } 37 } 38 return found; 39 } 40 41 describe("ASRTargeting docs", () => { 42 const DOCS_TARGETING_HEADINGS = getHeadingsFromDocs(); 43 const DOCS_TOC = getTOCFromDocs(); 44 const ASRTargetingAttributes = [ 45 ...Object.keys(ASRouterTargeting.Environment).filter( 46 attribute => !SKIP_DOCS.includes(attribute) 47 ), 48 ...MESSAGE_CONTEXT_ATTRIBUTES, 49 ]; 50 51 describe("All targeting params documented in targeting-attributes.md", () => { 52 for (const targetingParam of ASRTargetingAttributes) { 53 // If this test is failing, you probably forgot to add docs to asrouter/docs/targeting-attributes.md 54 // for a new targeting attribute, or you forgot to put it in the table of contents up top. 55 it(`should have docs and table of contents entry for ${targetingParam}`, () => { 56 assert.include( 57 DOCS_TARGETING_HEADINGS, 58 targetingParam, 59 `Didn't find the heading: ### \`${targetingParam}\`` 60 ); 61 assert.include( 62 DOCS_TOC, 63 targetingParam, 64 `Didn't find a table of contents entry for ${targetingParam}` 65 ); 66 }); 67 } 68 }); 69 describe("No extra attributes in targeting-attributes.md", () => { 70 // "allow" includes targeting attributes that are not implemented by 71 // ASRTargetingAttributes. For example trigger context passed to the evaluation 72 // context in when a trigger runs or ASRouter state used in the evaluation. 73 const allow = [ 74 "messageImpressions", 75 "screenImpressions", 76 "browserIsSelected", 77 "isAIWindow", 78 ]; 79 for (const targetingParam of DOCS_TARGETING_HEADINGS.filter( 80 doc => !allow.includes(doc) 81 )) { 82 // If this test is failing, you might have spelled something wrong or removed a targeting param without 83 // removing its docs. 84 it(`should have an implementation for ${targetingParam} in ASRouterTargeting.Environment`, () => { 85 assert.include( 86 ASRTargetingAttributes, 87 targetingParam, 88 `Didn't find an implementation for ${targetingParam}` 89 ); 90 }); 91 } 92 }); 93 });