Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tests/install-id-fixtures/** binary
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,10 @@ port, which can be told to hang or to answer `401`/`500`. The type-check
deliberately has no Node types available: if `trace-client.ts` compiles
against `ES2022` + `DOM` alone, it uses nothing the Edge runtime lacks.

The install-ID file fixtures in `tests/install-id-fixtures` (with what the
loader does to each in `expected.txt`) are frozen once released: never edit
or delete one. A new case is a new fixture plus its line in `expected.txt`.

## License

MIT.
77 changes: 77 additions & 0 deletions tests/install-id-fixtures.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// The install-ID compatibility test: freezes what TraceClient.installIdFromFile
// does with every file in tests/install-id-fixtures, as listed in expected.txt.
// An ID a released version stored must keep loading as the same ID, and a file
// the loader cannot read must never be overwritten. Fixtures and their
// `loaded`/`kept` lines are frozen once released: never edit or delete one; a
// new case is a new fixture plus its line in expected.txt.
import { describe, it } from "node:test";
import assert from "node:assert/strict";
import { copyFileSync, mkdtempSync, readFileSync, readdirSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { fileURLToPath } from "node:url";

import { TraceClient } from "../trace-client.ts";

const FIXTURES = fileURLToPath(new URL("./install-id-fixtures/", import.meta.url));
const EXPECTED = join(FIXTURES, "expected.txt");
const UUID4 = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;

type Expectation = { file: string; outcome: "loaded"; id: string } | { file: string; outcome: "replaced" | "kept" };

function readExpected(): Expectation[] {
const expectations: Expectation[] = [];
for (const raw of readFileSync(EXPECTED, "utf8").split(/\r?\n/)) {
const line = raw.trim();
if (!line || line.startsWith("#")) continue;
const [file, outcome, id, ...rest] = line.split(/\s+/);
assert.equal(rest.length, 0, `expected.txt: too many fields in ${JSON.stringify(line)}`);
if (outcome === "loaded") {
assert.ok(id, `expected.txt: no ID on ${JSON.stringify(line)}`);
expectations.push({ file, outcome, id });
} else if (outcome === "replaced" || outcome === "kept") {
assert.equal(id, undefined, `expected.txt: stray field on ${JSON.stringify(line)}`);
expectations.push({ file, outcome });
} else {
assert.fail(`expected.txt: unknown outcome on ${JSON.stringify(line)}`);
}
}
return expectations;
}

const expectations = readExpected();

describe("install-ID fixtures (tests/install-id-fixtures/expected.txt)", () => {
it("lists exactly the *.id files in the directory", () => {
const onDisk = readdirSync(FIXTURES).filter((name) => name.endsWith(".id")).sort();
const listed = expectations.map((e) => e.file).sort();
assert.deepEqual(listed, onDisk);
assert.equal(new Set(listed).size, listed.length, "a fixture is listed twice");
});

for (const expectation of expectations) {
it(`${expectation.file} is ${expectation.outcome}`, () => {
const dir = mkdtempSync(join(tmpdir(), "trace-install-id-fixture-"));
try {
const path = join(dir, expectation.file);
copyFileSync(join(FIXTURES, expectation.file), path);
const before = readFileSync(path);
const returned = TraceClient.installIdFromFile(path);
const after = readFileSync(path);
if (expectation.outcome === "loaded") {
assert.equal(returned, expectation.id);
assert.ok(after.equals(before), "the file must be left untouched");
} else if (expectation.outcome === "replaced") {
assert.match(returned, UUID4);
assert.ok(after.equals(Buffer.from(returned + "\n", "utf8")), "the file must now hold exactly the new ID and a newline");
} else {
assert.ok(after.equals(before), "an unreadable file must be left byte-identical");
assert.match(returned, UUID4);
assert.ok(!before.includes(Buffer.from(returned, "utf8")), "the fresh ID must not come from the file");
}
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
}
});
1 change: 1 addition & 0 deletions tests/install-id-fixtures/bad-chars.id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
not an id!
1 change: 1 addition & 0 deletions tests/install-id-fixtures/bom.id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bom-id
2 changes: 2 additions & 0 deletions tests/install-id-fixtures/comment-first.id
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# install id
real-id
1 change: 1 addition & 0 deletions tests/install-id-fixtures/created.id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3f2b8c1e-7a4d-4e6f-9b2a-5c8d1e0f7a6b
1 change: 1 addition & 0 deletions tests/install-id-fixtures/crlf.id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
crlf-id
Empty file.
27 changes: 27 additions & 0 deletions tests/install-id-fixtures/expected.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Install-ID file fixtures: what TraceClient.installIdFromFile does with each file here.
# One line per fixture, sorted by file name: `<file> loaded <id>` | `<file> replaced` | `<file> kept`.
# loaded <id>: the loader returns exactly that ID and leaves the file untouched.
# replaced: the file holds no ID, so the loader writes a fresh lowercase uuid4 plus "\n" and returns it.
# kept: the file cannot be read, so the loader leaves it byte-identical and returns a fresh uuid4 for this process only.
# Measured against the 0.5.0 release. Once released, a loaded or kept line must never change, and no fixture may be
# edited or deleted; a new case is a new fixture plus its line here.
bad-chars.id replaced
bom.id loaded bom-id
comment-first.id loaded real-id
created.id loaded 3f2b8c1e-7a4d-4e6f-9b2a-5c8d1e0f7a6b
crlf.id loaded crlf-id
empty.id replaced
first-of-two.id loaded first-id
id-then-not-utf8.id loaded good-id
invalid-lines-first.id loaded third.is-valid
leading-blank-lines.id loaded after-blanks
max-length.id loaded aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
non-ascii.id replaced
not-utf8.id replaced
padded.id loaded padded-id
plain.id loaded plain-id
tabs.id loaded tabbed_id
too-long.id replaced
trailing-newline.id loaded with.newline_1
uppercase-uuid.id loaded 0F8FAD5B-D9CB-469F-A165-70867728950E
whitespace-only.id replaced
2 changes: 2 additions & 0 deletions tests/install-id-fixtures/first-of-two.id
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
first-id
second-id
2 changes: 2 additions & 0 deletions tests/install-id-fixtures/id-then-not-utf8.id
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
good-id
ÿþ
4 changes: 4 additions & 0 deletions tests/install-id-fixtures/invalid-lines-first.id
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
has space
not/valid
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
third.is-valid
5 changes: 5 additions & 0 deletions tests/install-id-fixtures/leading-blank-lines.id
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@




after-blanks
1 change: 1 addition & 0 deletions tests/install-id-fixtures/max-length.id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
1 change: 1 addition & 0 deletions tests/install-id-fixtures/non-ascii.id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
café-id
Binary file added tests/install-id-fixtures/not-utf8.id
Binary file not shown.
1 change: 1 addition & 0 deletions tests/install-id-fixtures/padded.id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
padded-id
1 change: 1 addition & 0 deletions tests/install-id-fixtures/plain.id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
plain-id
1 change: 1 addition & 0 deletions tests/install-id-fixtures/tabs.id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tabbed_id
1 change: 1 addition & 0 deletions tests/install-id-fixtures/too-long.id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
1 change: 1 addition & 0 deletions tests/install-id-fixtures/trailing-newline.id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
with.newline_1
1 change: 1 addition & 0 deletions tests/install-id-fixtures/uppercase-uuid.id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0F8FAD5B-D9CB-469F-A165-70867728950E
2 changes: 2 additions & 0 deletions tests/install-id-fixtures/whitespace-only.id
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@


3 changes: 2 additions & 1 deletion tests/trace-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1286,7 +1286,8 @@ describe("API shape", () => {
assert.equal(await client.flush(50), undefined);
assert.equal(await client.close(50), undefined);
assert.equal(TraceClient.disabled().enabled, false);
assert.equal(TRACE_CLIENT_VERSION, "0.5.0");
const packageJson = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8")) as { version: string };
assert.equal(TRACE_CLIENT_VERSION, packageJson.version);
});

it("keeps the documented values of its public constants, which the Python and Java clients share", () => {
Expand Down
37 changes: 37 additions & 0 deletions tests/version-consistency.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Every place the version is written must agree with package.json, each found
// exactly once, so a release can never ship with two different versions.
import { describe, it } from "node:test";
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";

const read = (name: string): string => readFileSync(new URL(`../${name}`, import.meta.url), "utf8");
const escape = (text: string): string => text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");

const version = (JSON.parse(read("package.json")) as { version: string }).version;

function occurrences(text: string, needle: string): number {
return text.match(new RegExp(escape(needle), "g"))?.length ?? 0;
}

describe("version consistency", () => {
it("package.json has a version", () => {
assert.match(version, /^\d+\.\d+\.\d+(?:[-+].*)?$/);
});

it("both root entries of package-lock.json match package.json", () => {
const lock = JSON.parse(read("package-lock.json")) as { version: string; packages: Record<string, { version?: string }> };
assert.equal(lock.version, version);
assert.equal(lock.packages[""]?.version, version);
});

const spots: Array<[file: string, needle: string]> = [
["trace-client.ts", `trace-client ${version} --`],
["trace-client.ts", `TRACE_CLIENT_VERSION = "${version}"`],
["README.md", `trace-client-js/${version} (`],
];
for (const [file, needle] of spots) {
it(`${file} says ${JSON.stringify(needle)} exactly once`, () => {
assert.equal(occurrences(read(file), needle), 1);
});
}
});
Loading