summaryrefslogtreecommitdiff
path: root/packages/cli/src/test-utils/customMatchers.ts
diff options
context:
space:
mode:
authorJacob Richman <[email protected]>2025-07-31 16:16:29 -0700
committerGitHub <[email protected]>2025-07-31 23:16:29 +0000
commit61e382444a69409b066a6c8382379f86492d579f (patch)
tree9f66858195607aea50eb421a581d5cd934fe56b9 /packages/cli/src/test-utils/customMatchers.ts
parent32809a7be795b974506b893a179091a83b285b7b (diff)
fix(ux) bug in replaceRange dealing with newLines that was breaking vim support (#5320)
Diffstat (limited to 'packages/cli/src/test-utils/customMatchers.ts')
-rw-r--r--packages/cli/src/test-utils/customMatchers.ts63
1 files changed, 63 insertions, 0 deletions
diff --git a/packages/cli/src/test-utils/customMatchers.ts b/packages/cli/src/test-utils/customMatchers.ts
new file mode 100644
index 00000000..c0b4df6b
--- /dev/null
+++ b/packages/cli/src/test-utils/customMatchers.ts
@@ -0,0 +1,63 @@
+/**
+ * @license
+ * Copyright 2025 Google LLC
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+/// <reference types="vitest/globals" />
+
+/**
+ * @license
+ * Copyright 2025 Google LLC
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+import { expect } from 'vitest';
+import type { TextBuffer } from '../ui/components/shared/text-buffer.js';
+
+// RegExp to detect invalid characters: backspace, and ANSI escape codes
+// eslint-disable-next-line no-control-regex
+const invalidCharsRegex = /[\b\x1b]/;
+
+function toHaveOnlyValidCharacters(this: vi.Assertion, buffer: TextBuffer) {
+ const { isNot } = this;
+ let pass = true;
+ const invalidLines: Array<{ line: number; content: string }> = [];
+
+ for (let i = 0; i < buffer.lines.length; i++) {
+ const line = buffer.lines[i];
+ if (line.includes('\n')) {
+ pass = false;
+ invalidLines.push({ line: i, content: line });
+ break; // Fail fast on newlines
+ }
+ if (invalidCharsRegex.test(line)) {
+ pass = false;
+ invalidLines.push({ line: i, content: line });
+ }
+ }
+
+ return {
+ pass,
+ message: () =>
+ `Expected buffer ${isNot ? 'not ' : ''}to have only valid characters, but found invalid characters in lines:\n${invalidLines
+ .map((l) => ` [${l.line}]: "${l.content}"`) /* This line was changed */
+ .join('\n')}`,
+ actual: buffer.lines,
+ expected: 'Lines with no line breaks, backspaces, or escape codes.',
+ };
+}
+
+expect.extend({
+ toHaveOnlyValidCharacters,
+});
+
+// Extend Vitest's `expect` interface with the custom matcher's type definition.
+declare module 'vitest' {
+ interface Assertion<T> {
+ toHaveOnlyValidCharacters(): T;
+ }
+ interface AsymmetricMatchersContaining {
+ toHaveOnlyValidCharacters(): void;
+ }
+}