|
| 1 | +# SPDX-FileCopyrightText: 2025 diffo contributors <https://github.com/diffo-dev/diffo/graphs.contributors> |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +defmodule Diffo.Provider.VersioningTest do |
| 6 | + @moduledoc false |
| 7 | + use ExUnit.Case |
| 8 | + |
| 9 | + alias Diffo.Test.Servo |
| 10 | + alias Diffo.Test.Broadband |
| 11 | + alias Diffo.Test.BroadbandV2 |
| 12 | + |
| 13 | + setup_all do |
| 14 | + AshNeo4j.BoltyHelper.start() |
| 15 | + end |
| 16 | + |
| 17 | + setup do |
| 18 | + on_exit(fn -> |
| 19 | + AshNeo4j.Neo4jHelper.delete_all() |
| 20 | + end) |
| 21 | + end |
| 22 | + |
| 23 | + describe "minor version — backward-compatible change" do |
| 24 | + # A minor version represents a non-breaking change such as adding a new technology type. |
| 25 | + # The specification node is updated in place — no migration of any kind is required. |
| 26 | + # All existing instances immediately reflect the new version. |
| 27 | + |
| 28 | + test "minor version bump updates the specification node to v1.1.0" do |
| 29 | + Servo.build_broadband(%{}) |
| 30 | + {:ok, spec} = Diffo.Provider.get_specification_by_id(Broadband.specification()[:id]) |
| 31 | + assert spec.version == "v1.0.0" |
| 32 | + |
| 33 | + minored = Diffo.Provider.next_minor_specification!(spec) |
| 34 | + assert minored.version == "v1.1.0" |
| 35 | + end |
| 36 | + |
| 37 | + test "all existing V1 instances immediately reflect the new minor version" do |
| 38 | + {:ok, v1_a} = Servo.build_broadband(%{}) |
| 39 | + {:ok, v1_b} = Servo.build_broadband(%{}) |
| 40 | + |
| 41 | + {:ok, spec} = Diffo.Provider.get_specification_by_id(Broadband.specification()[:id]) |
| 42 | + Diffo.Provider.next_minor_specification!(spec) |
| 43 | + |
| 44 | + {:ok, reloaded_a} = Diffo.Provider.get_instance_by_id(v1_a.id) |
| 45 | + {:ok, reloaded_b} = Diffo.Provider.get_instance_by_id(v1_b.id) |
| 46 | + assert reloaded_a.specification.version == "v1.1.0" |
| 47 | + assert reloaded_b.specification.version == "v1.1.0" |
| 48 | + end |
| 49 | + |
| 50 | + test "minor version freeze — removing behaviour do blocks creation without a new module" do |
| 51 | + # When NBN removes behaviour do from Broadband and deploys v1.1, build_broadband |
| 52 | + # disappears from the domain API. This is the machine-readable announcement of the freeze. |
| 53 | + # Existing instances are unaffected; all other operations continue via the module. |
| 54 | + # This cannot be demonstrated in a single test suite since the module is fixed at |
| 55 | + # compile time, but the mechanism is proven by the BroadbandV1_1 fixture pattern: |
| 56 | + # same spec id, no behaviour do block, no build wired in the domain. |
| 57 | + assert Broadband.specification()[:id] == "a1b2c3d4-e5f6-4a7b-8c9d-e0f1a2b3c4d5" |
| 58 | + assert function_exported?(Diffo.Test.Servo, :build_broadband, 2) |
| 59 | + refute function_exported?(Diffo.Test.Servo, :build_broadband_v1_1, 2) |
| 60 | + end |
| 61 | + end |
| 62 | + |
| 63 | + describe "major version — concurrent V1 and V2" do |
| 64 | + test "V1 and V2 specifications coexist with same name and different major_version" do |
| 65 | + Servo.build_broadband(%{}) |
| 66 | + Servo.build_broadband_v2(%{}) |
| 67 | + |
| 68 | + specs = Diffo.Provider.find_specifications_by_name!("broadband") |
| 69 | + assert length(specs) == 2 |
| 70 | + |
| 71 | + versions = Enum.map(specs, & &1.major_version) |> Enum.sort() |
| 72 | + assert versions == [1, 2] |
| 73 | + end |
| 74 | + |
| 75 | + test "V1 instance is linked to V1 specification" do |
| 76 | + {:ok, v1} = Servo.build_broadband(%{}) |
| 77 | + assert v1.specification_id == Broadband.specification()[:id] |
| 78 | + end |
| 79 | + |
| 80 | + test "V2 instance is linked to V2 specification" do |
| 81 | + {:ok, v2} = Servo.build_broadband_v2(%{}) |
| 82 | + assert v2.specification_id == BroadbandV2.specification()[:id] |
| 83 | + end |
| 84 | + |
| 85 | + test "V1 and V2 instances operate concurrently" do |
| 86 | + {:ok, v1} = Servo.build_broadband(%{}) |
| 87 | + {:ok, v2} = Servo.build_broadband_v2(%{}) |
| 88 | + |
| 89 | + v1_instances = Diffo.Provider.find_instances_by_specification_id!(Broadband.specification()[:id]) |
| 90 | + v2_instances = Diffo.Provider.find_instances_by_specification_id!(BroadbandV2.specification()[:id]) |
| 91 | + |
| 92 | + assert length(v1_instances) == 1 |
| 93 | + assert length(v2_instances) == 1 |
| 94 | + assert v1.specification_id != v2.specification_id |
| 95 | + end |
| 96 | + end |
| 97 | + |
| 98 | + describe "major version — RSP migration from V1 to V2" do |
| 99 | + # V2 must be published (specification node created) before any instance can be |
| 100 | + # respecified to it. Building the first V2 instance is what publishes the specification. |
| 101 | + setup do |
| 102 | + {:ok, _} = Servo.build_broadband_v2(%{}) |
| 103 | + :ok |
| 104 | + end |
| 105 | + |
| 106 | + test "V1 instance is respecified to V2 via respecify_instance" do |
| 107 | + {:ok, v1} = Servo.build_broadband(%{}) |
| 108 | + {:ok, instance} = Diffo.Provider.get_instance_by_id(v1.id) |
| 109 | + |
| 110 | + {:ok, migrated} = Diffo.Provider.respecify_instance(instance, %{ |
| 111 | + specified_by: BroadbandV2.specification()[:id] |
| 112 | + }) |
| 113 | + |
| 114 | + assert migrated.specification.id == BroadbandV2.specification()[:id] |
| 115 | + end |
| 116 | + |
| 117 | + test "migrated instance is found by V2 specification" do |
| 118 | + {:ok, v1} = Servo.build_broadband(%{}) |
| 119 | + {:ok, instance} = Diffo.Provider.get_instance_by_id(v1.id) |
| 120 | + {:ok, _} = Diffo.Provider.respecify_instance(instance, %{ |
| 121 | + specified_by: BroadbandV2.specification()[:id] |
| 122 | + }) |
| 123 | + |
| 124 | + v2_instances = Diffo.Provider.find_instances_by_specification_id!(BroadbandV2.specification()[:id]) |
| 125 | + assert Enum.any?(v2_instances, &(&1.id == v1.id)) |
| 126 | + end |
| 127 | + |
| 128 | + test "migrated instance is no longer found by V1 specification" do |
| 129 | + {:ok, v1} = Servo.build_broadband(%{}) |
| 130 | + {:ok, instance} = Diffo.Provider.get_instance_by_id(v1.id) |
| 131 | + {:ok, _} = Diffo.Provider.respecify_instance(instance, %{ |
| 132 | + specified_by: BroadbandV2.specification()[:id] |
| 133 | + }) |
| 134 | + |
| 135 | + v1_instances = Diffo.Provider.find_instances_by_specification_id!(Broadband.specification()[:id]) |
| 136 | + refute Enum.any?(v1_instances, &(&1.id == v1.id)) |
| 137 | + end |
| 138 | + |
| 139 | + test "V1 withdrawal — all V1 instances migrated, none remain on V1" do |
| 140 | + {:ok, v1_a} = Servo.build_broadband(%{}) |
| 141 | + {:ok, v1_b} = Servo.build_broadband(%{}) |
| 142 | + |
| 143 | + {:ok, instance_a} = Diffo.Provider.get_instance_by_id(v1_a.id) |
| 144 | + {:ok, instance_b} = Diffo.Provider.get_instance_by_id(v1_b.id) |
| 145 | + {:ok, _} = Diffo.Provider.respecify_instance(instance_a, %{specified_by: BroadbandV2.specification()[:id]}) |
| 146 | + {:ok, _} = Diffo.Provider.respecify_instance(instance_b, %{specified_by: BroadbandV2.specification()[:id]}) |
| 147 | + |
| 148 | + assert Diffo.Provider.find_instances_by_specification_id!(Broadband.specification()[:id]) == [] |
| 149 | + assert length(Diffo.Provider.find_instances_by_specification_id!(BroadbandV2.specification()[:id])) == 3 |
| 150 | + end |
| 151 | + end |
| 152 | +end |
0 commit comments