--- /dev/null
+using Content.Shared.Roles;
+using Content.Shared.Roles.Jobs;
+using Robust.Shared.Prototypes;
+using System.Linq;
+
+namespace Content.IntegrationTests.Tests.Station;
+
+[TestFixture]
+[TestOf(typeof(SharedJobSystem))]
+public sealed class JobTest
+{
+ /// <summary>
+ /// Ensures that every job belongs to at most 1 primary department.
+ /// Having no primary department is ok.
+ /// </summary>
+ [Test]
+ public async Task PrimaryDepartmentsTest()
+ {
+ await using var pair = await PoolManager.GetServerClient();
+ var server = pair.Server;
+
+ var prototypeManager = server.ResolveDependency<IPrototypeManager>();
+
+ await server.WaitAssertion(() =>
+ {
+ // only checking primary departments so don't bother with others
+ var departments = prototypeManager.EnumeratePrototypes<DepartmentPrototype>()
+ .Where(department => department.Primary)
+ .ToList();
+ var jobs = prototypeManager.EnumeratePrototypes<JobPrototype>();
+ foreach (var job in jobs)
+ {
+ // not actually using the jobs system since that will return the first department
+ // and we need to test that there is never more than 1, so it not sorting them is correct
+ var primaries = 0;
+ foreach (var department in departments)
+ {
+ if (!department.Roles.Contains(job.ID))
+ continue;
+
+ primaries++;
+ Assert.That(primaries, Is.EqualTo(1), $"The job {job.ID} has more than 1 primary department!");
+ }
+ }
+ });
+ await pair.CleanReturnAsync();
+ }
+}
[ViewVariables(VVAccess.ReadWrite),
DataField("roles", customTypeSerializer: typeof(PrototypeIdListSerializer<JobPrototype>))]
public List<string> Roles = new();
+
+ /// <summary>
+ /// Whether this is a primary department or not.
+ /// For example, CE's primary department is engineering since Command has primary: false.
+ /// </summary>
+ [DataField, ViewVariables(VVAccess.ReadWrite)]
+ public bool Primary = true;
}
return false;
}
+ /// <summary>
+ /// Like <see cref="TryGetDepartment"/> but ignores any non-primary departments.
+ /// For example, with CE it will return Engineering but with captain it will
+ /// not return anything, since Command is not a primary department.
+ /// </summary>
+ public bool TryGetPrimaryDepartment(string jobProto, [NotNullWhen(true)] out DepartmentPrototype? departmentPrototype)
+ {
+ // not sorting it since there should only be 1 primary department for a job.
+ // this is enforced by the job tests.
+ var departmentProtos = _protoManager.EnumeratePrototypes<DepartmentPrototype>();
+
+ foreach (var department in departmentProtos)
+ {
+ if (department.Primary && department.Roles.Contains(jobProto))
+ {
+ departmentPrototype = department;
+ return true;
+ }
+ }
+
+ departmentPrototype = null;
+ return false;
+ }
+
public bool MindHasJobWithId(EntityUid? mindId, string prototypeId)
{
return CompOrNull<JobComponent>(mindId)?.Prototype == prototypeId;