fix: incorporate feedback

This commit is contained in:
Patrick Erichsen 2025-05-12 18:31:56 -07:00
parent 372fd87ffb
commit 98efbbd80f
3 changed files with 90 additions and 4 deletions

View file

@ -0,0 +1,24 @@
name: unit-testing-rules
version: 0.0.1
schema: v1
rules:
- name: unit-testing-rules
rule: >-
For unit testing in this project:
1. The project uses Jest as the testing framework
2. Run tests using `npm test` from within the specific package/module
directory
3. Command structure: `cd [directory] && npm test -- [test file path]`
4. The test script uses experimental VM modules via NODE_OPTIONS flag
5. Test files follow the pattern `*.test.ts`
6. Tests must import Jest with `import { jest } from "@jest/globals";`
7. Run tests from within the specific package directory (e.g., `cd core`
for core module tests)

View file

@ -0,0 +1,58 @@
import { IndexTag } from "..";
import { tagToString } from "./utils";
test("tagToString returns full tag string when under length limit", () => {
const tag: IndexTag = {
directory: "/normal/path/to/repo",
branch: "main",
artifactId: "12345",
};
expect(tagToString(tag)).toBe("/normal/path/to/repo::main::12345");
});
test("tagToString truncates beginning of directory when path is too long", () => {
// Create a very long directory path that exceeds MAX_DIR_LENGTH (200)
const longPrefix = "/very/long/path/that/will/be/truncated/";
const importantSuffix = "/user/important-project/src/feature";
const longPath = longPrefix + "x".repeat(200) + importantSuffix;
const tag: IndexTag = {
directory: longPath,
branch: "feature-branch",
artifactId: "67890",
};
const result = tagToString(tag);
// The result should keep the important suffix part
expect(result).toContain(importantSuffix);
// The result should NOT contain the beginning of the path
expect(result).not.toContain(longPrefix);
// The result should include the branch and artifactId
expect(result).toContain("::feature-branch::67890");
// The result should be within the MAX_TABLE_NAME_LENGTH limit (240)
expect(result.length).toBeLessThanOrEqual(240);
});
test("tagToString preserves branch and artifactId exactly, even when truncating", () => {
const longPath = "/a".repeat(300); // Much longer than MAX_DIR_LENGTH
const tag: IndexTag = {
directory: longPath,
branch: "release-v2.0",
artifactId: "build-123",
};
const result = tagToString(tag);
// Should contain the exact branch and artifactId
expect(result).toContain("::release-v2.0::build-123");
// Should contain the end of the path
expect(result).toContain("/a/a/a");
// Should not contain the full original path (it should be truncated)
expect(result.length).toBeLessThan(
longPath.length + "::release-v2.0::build-123".length,
);
// The result should be within the MAX_TABLE_NAME_LENGTH limit
expect(result.length).toBeLessThanOrEqual(240);
});

View file

@ -3,6 +3,9 @@ import { IndexTag } from "..";
// Maximum length for table names to stay under OS filename limits
const MAX_TABLE_NAME_LENGTH = 240;
// Leave room for branch and artifactId
const MAX_DIR_LENGTH = 200;
/**
* Converts an IndexTag to a string representation, safely handling long paths.
*
@ -15,7 +18,8 @@ const MAX_TABLE_NAME_LENGTH = 240;
*
* To handle long paths:
* 1. First tries the full string - most backwards compatible
* 2. If too long, truncates directory to 200 chars to leave room for branch and artifactId
* 2. If too long, truncates directory from the beginning to maintain uniqueness
* (since final parts of paths are more unique than prefixes)
* 3. Finally ensures entire string stays under MAX_TABLE_NAME_LENGTH for OS compatibility
*
* @param tag The tag containing directory, branch, and artifactId
@ -28,10 +32,10 @@ export function tagToString(tag: IndexTag): string {
return result;
}
const maxDirLength = 200; // Leave room for branch and artifactId
// Truncate from the beginning of directory path to preserve the more unique end parts
const dir =
tag.directory.length > maxDirLength
? tag.directory.slice(0, maxDirLength)
tag.directory.length > MAX_DIR_LENGTH
? tag.directory.slice(tag.directory.length - MAX_DIR_LENGTH)
: tag.directory;
return `${dir}::${tag.branch}::${tag.artifactId}`.slice(