|
1 | 1 | jest.mock(`../event-storage`)
|
2 | 2 | import { EventStorage } from "../event-storage"
|
3 | 3 | import { AnalyticsTracker } from "../telemetry"
|
| 4 | +import * as fs from "fs-extra" |
| 5 | +import * as os from "os" |
| 6 | +import * as path from "path" |
| 7 | +import uuidv4 from "uuid/v4" |
4 | 8 |
|
5 | 9 | let telemetry
|
6 | 10 | beforeEach(() => {
|
@@ -57,4 +61,119 @@ describe(`Telemetry`, () => {
|
57 | 61 | )
|
58 | 62 | })
|
59 | 63 | })
|
| 64 | + |
| 65 | + describe(`allows reading tags from path`, () => { |
| 66 | + it(`getTagsFromPath should read the file and detect updates`, async () => { |
| 67 | + const t = new AnalyticsTracker({ |
| 68 | + componentId: `component`, |
| 69 | + }) |
| 70 | + |
| 71 | + // Test it when env not set |
| 72 | + let res = t.getTagsFromPath() |
| 73 | + expect(res).toMatchObject({}) |
| 74 | + expect(t.lastEnvTagsFromFileTime).toBe(0) |
| 75 | + |
| 76 | + // create file and write initial data |
| 77 | + const filePath = path.join(fs.realpathSync(os.tmpdir()), uuidv4()) |
| 78 | + console.log(filePath) |
| 79 | + process.env.GATSBY_TELEMETRY_METADATA_PATH = filePath |
| 80 | + |
| 81 | + fs.writeFileSync(filePath, JSON.stringify({ componentId: `test` })) |
| 82 | + await new Promise(resolve => { |
| 83 | + setTimeout(resolve, 2000) |
| 84 | + }) |
| 85 | + // get it and make sure we see it and the ts matches |
| 86 | + res = t.getTagsFromPath() |
| 87 | + expect(res).toMatchObject({ componentId: `test` }) |
| 88 | + let stat = fs.statSync(filePath) |
| 89 | + expect(t.lastEnvTagsFromFileTime).toBe(stat.mtimeMs) |
| 90 | + |
| 91 | + // Update the file |
| 92 | + fs.writeFileSync(filePath, JSON.stringify({ componentId: `test2` })) |
| 93 | + |
| 94 | + await new Promise(resolve => { |
| 95 | + setTimeout(resolve, 2000) |
| 96 | + }) |
| 97 | + stat = fs.statSync(filePath) |
| 98 | + // make sure we see the change |
| 99 | + res = t.getTagsFromPath() |
| 100 | + expect(t.lastEnvTagsFromFileTime).toBe(stat.mtimeMs) |
| 101 | + expect(res).toMatchObject({ componentId: `test2` }) |
| 102 | + |
| 103 | + // read it with out updating |
| 104 | + res = t.getTagsFromPath() |
| 105 | + expect(t.lastEnvTagsFromFileTime).toBe(stat.mtimeMs) |
| 106 | + expect(res).toMatchObject({ componentId: `test2` }) |
| 107 | + fs.unlinkSync(filePath) |
| 108 | + |
| 109 | + const filePath2 = path.join(fs.realpathSync(os.tmpdir()), uuidv4()) |
| 110 | + process.env.GATSBY_TELEMETRY_METADATA_PATH = filePath2 |
| 111 | + res = t.getTagsFromPath() |
| 112 | + expect(t.lastEnvTagsFromFileTime).toBe(stat.mtimeMs) |
| 113 | + expect(res).toMatchObject({}) |
| 114 | + }, 10000) |
| 115 | + }) |
| 116 | + |
| 117 | + it(`getTagsFromPath is used for buildEvent`, async () => { |
| 118 | + const t = new AnalyticsTracker({ |
| 119 | + componentId: `component`, |
| 120 | + }) |
| 121 | + t.buildAndStoreEvent(`demo`, {}) |
| 122 | + expect( |
| 123 | + (EventStorage as jest.Mock).mock.instances[1].addEvent |
| 124 | + ).toHaveBeenCalledWith( |
| 125 | + expect.objectContaining({ |
| 126 | + eventType: `demo`, |
| 127 | + componentId: `component`, |
| 128 | + }) |
| 129 | + ) |
| 130 | + const filePath = path.join(fs.realpathSync(os.tmpdir()), uuidv4()) |
| 131 | + process.env.GATSBY_TELEMETRY_METADATA_PATH = filePath |
| 132 | + fs.writeFileSync(filePath, JSON.stringify({ componentId: `test` })) |
| 133 | + await new Promise(resolve => { |
| 134 | + setTimeout(resolve, 2000) |
| 135 | + }) |
| 136 | + const stat = fs.statSync(filePath) |
| 137 | + t.buildAndStoreEvent(`demo2`, {}) |
| 138 | + |
| 139 | + expect(t.lastEnvTagsFromFileTime).toBe(stat.mtimeMs) |
| 140 | + expect( |
| 141 | + (EventStorage as jest.Mock).mock.instances[1].addEvent |
| 142 | + ).toHaveBeenCalledWith( |
| 143 | + expect.objectContaining({ |
| 144 | + eventType: `demo2`, |
| 145 | + componentId: `test`, |
| 146 | + }) |
| 147 | + ) |
| 148 | + |
| 149 | + t.buildAndStoreEvent(`demo3`, {}) |
| 150 | + expect( |
| 151 | + (EventStorage as jest.Mock).mock.instances[1].addEvent |
| 152 | + ).toHaveBeenCalledWith( |
| 153 | + expect.objectContaining({ |
| 154 | + eventType: `demo3`, |
| 155 | + componentId: `test`, |
| 156 | + }) |
| 157 | + ) |
| 158 | + |
| 159 | + expect(t.lastEnvTagsFromFileTime).toBe(stat.mtimeMs) |
| 160 | + |
| 161 | + fs.writeFileSync(filePath, JSON.stringify({ componentId: `4` })) |
| 162 | + await new Promise(resolve => { |
| 163 | + setTimeout(resolve, 2000) |
| 164 | + }) |
| 165 | + const stat2 = fs.statSync(filePath) |
| 166 | + |
| 167 | + t.buildAndStoreEvent(`demo4`, {}) |
| 168 | + expect(t.lastEnvTagsFromFileTime).toBe(stat2.mtimeMs) |
| 169 | + expect( |
| 170 | + (EventStorage as jest.Mock).mock.instances[1].addEvent |
| 171 | + ).toHaveBeenCalledWith( |
| 172 | + expect.objectContaining({ |
| 173 | + eventType: `demo4`, |
| 174 | + componentId: `4`, |
| 175 | + }) |
| 176 | + ) |
| 177 | + fs.unlinkSync(filePath) |
| 178 | + }, 10000) |
60 | 179 | })
|
0 commit comments