Skip to content

Commit

Permalink
Add test case.
Browse files Browse the repository at this point in the history
  • Loading branch information
tjprescott committed Jun 4, 2024
1 parent 40a77ef commit 82f5376
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
28 changes: 25 additions & 3 deletions packages/versioning/src/versioning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,15 @@ function getParentAddedVersionInTimeline(
return undefined;
}

/**
* Returns true if the first version modifier was @added, false if @removed.
*/
function resolveAddedFirst(added: Version[], removed: Version[]): boolean {
if (added.length === 0) return false;
if (removed.length === 0) return true;
return added[0].index < removed[0].index;
}

export function getAvailabilityMap(
program: Program,
type: Type
Expand All @@ -261,9 +270,15 @@ export function getAvailabilityMap(
)
return undefined;

const wasAddedFirst = resolveAddedFirst(added, removed);

// implicitly, all versioned things are assumed to have been added at
// v1 if not specified, or inherited from their parent.
if (!added.length && !parentAdded) {
if (!wasAddedFirst && !parentAdded) {
// if the first version modifier was @removed, and the parent is implicitly available,
// then assume the type was available.
added = [allVersions[0], ...added];
} else if (!added.length && !parentAdded) {
// no version information on the item or its parent implicitly means it has always been available
added.push(allVersions[0]);
} else if (!added.length && parentAdded) {
Expand Down Expand Up @@ -317,11 +332,18 @@ export function getAvailabilityMapInTimeline(
)
return undefined;

const wasAddedFirst = resolveAddedFirst(added, removed);

// implicitly, all versioned things are assumed to have been added at
// v1 if not specified, or inherited from their parent.
if (!added.length && !parentAdded) {
const firstVersion = timeline.first().versions().next().value;
if (!wasAddedFirst && !parentAdded) {
// if the first version modifier was @removed, and the parent is implicitly available,
// then assume the type was available.
added = [firstVersion, ...added];
} else if (!added.length && !parentAdded) {
// no version information on the item or its parent implicitly means it has always been available
added.push(timeline.first().versions().next().value);
added.push(firstVersion);
} else if (!added.length && parentAdded) {
// if no version info on type but is on parent, inherit that parent's "added" version
added.push(parentAdded);
Expand Down
31 changes: 30 additions & 1 deletion packages/versioning/test/versioning.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ describe("versioning: logic", () => {
);
});

it("can be removed respecting model versioning", async () => {
it("can be removed respecting model versioning with explicit versions", async () => {
const {
source,
projections: [v2, v3, v4],
Expand Down Expand Up @@ -311,6 +311,35 @@ describe("versioning: logic", () => {
);
});

it("can be removed respecting model versioning with implicit versions", async () => {
const {
source,
projections: [v1, v2, v3],
} = await versionedModel(
["v1", "v2", "v3"],
`model Test {
a: int32;
@removed(Versions.v2)
@added(Versions.v3)
b: int32;
}
`
);

assertHasProperties(v1, ["a", "b"]);
assertHasProperties(v2, ["a"]);
assertHasProperties(v3, ["a", "b"]);

assertModelProjectsTo(
[
[v1, "v1"],
[v2, "v2"],
[v3, "v3"],
],
source
);
});

it("can be renamed", async () => {
const {
source,
Expand Down

0 comments on commit 82f5376

Please sign in to comment.