11// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
22
33import { describe , it , expect } from 'vitest' ;
4+ import { composeStacks , defineStack } from '@objectstack/spec' ;
45import {
56 validateChartBindings as validateChartBindingsUnrecorded ,
67 CHART_DIMENSION_UNKNOWN ,
@@ -9,6 +10,7 @@ import {
910 CHART_AXIS_NOT_SELECTED ,
1011} from './validate-chart-bindings.js' ;
1112import { explainRule } from './rule-explanations.js' ;
13+ import { runAuthoringRules } from './authoring-rules.js' ;
1214import { runRuntimeAuthoringRules } from './runtime-gate.js' ;
1315import { REFERENCE_INTEGRITY_RULES } from './reference-integrity-suite.js' ;
1416
@@ -1176,6 +1178,201 @@ describe('validateChartBindings — dataset-bound page chart components', () =>
11761178 } ) ;
11771179} ) ;
11781180
1181+ // [#33128, ADR-0150 D3] The CLI's per-package pass (`packageBodyAsStack`)
1182+ // hands each rule one package body, its own manifest, and the artifact's
1183+ // `packages[]` as resolution context. A chart binding's `dataset` resolves as
1184+ // the runtime resolves it for that package (every surface here sends its
1185+ // item's served package as `callerPackage`): its own datasets, then the ONE
1186+ // holder in its declared dependency closure — the resolution the dashboard
1187+ // rule reads (`indexArtifactDatasetResolution`). BASE (main 47f6c939): the
1188+ // declared-dependency report below was refused `chart-dataset-unknown`
1189+ // ("Declared datasets: (none).") while the runtime served it.
1190+ describe ( '[#33128] chart-dataset-unknown resolves through the reading package’s declared closure' , ( ) => {
1191+ const MODULE = 'com.example.sales' ;
1192+ const LIBRARY = 'com.example.library' ;
1193+ const SECOND = 'com.example.second' ;
1194+ const ELSEWHERE = 'com.example.elsewhere' ;
1195+
1196+ const libraryDataset = ( ) => ( {
1197+ name : 'book_metrics' ,
1198+ object : 'lib_book' ,
1199+ dimensions : [ { name : 'genre' , field : 'genre' } ] ,
1200+ measures : [ { name : 'book_count' , aggregate : 'count' } ] ,
1201+ } ) ;
1202+ const holder = ( id : string , extra : Record < string , unknown > = { } ) => ( {
1203+ id,
1204+ name : id . split ( '.' ) . pop ( ) ,
1205+ objects : [ { name : 'lib_book' , fields : { genre : { type : 'text' } } } ] ,
1206+ datasets : [ libraryDataset ( ) ] ,
1207+ ...extra ,
1208+ } ) ;
1209+ const report = ( overrides : Record < string , unknown > = { } ) => ( {
1210+ name : 'books_by_genre' ,
1211+ dataset : 'book_metrics' ,
1212+ rows : [ 'genre' ] ,
1213+ values : [ 'book_count' ] ,
1214+ chart : { type : 'bar' , xAxis : 'genre' , yAxis : 'book_count' } ,
1215+ ...overrides ,
1216+ } ) ;
1217+ const moduleBody = ( dependencies : string [ ] | undefined , extra : Record < string , unknown > = { } ) => ( {
1218+ id : MODULE ,
1219+ name : 'sales' ,
1220+ ...( dependencies ? { dependencies : Object . fromEntries ( dependencies . map ( ( d ) => [ d , '^1.0.0' ] ) ) } : { } ) ,
1221+ reports : [ report ( ) ] ,
1222+ ...extra ,
1223+ } ) ;
1224+ /** `packageBodyAsStack`'s shape: the body, its own manifest, the artifact's entries. */
1225+ const asPackageStack = ( body : Record < string , unknown > , siblings : Record < string , unknown > [ ] ) => ( {
1226+ ...body ,
1227+ manifest : body ,
1228+ packages : [ body , ...siblings ] . map ( ( manifest ) => ( { manifest } ) ) ,
1229+ } ) ;
1230+ const datasetUnknown = ( stack : Record < string , unknown > ) =>
1231+ validateChartBindings ( stack ) . filter ( ( f ) => f . rule === CHART_DATASET_UNKNOWN ) ;
1232+
1233+ it ( 'ACCEPTS a report over the dataset of a package it declares, and judges its positions against it' , ( ) => {
1234+ expect ( validateChartBindings ( asPackageStack ( moduleBody ( [ LIBRARY ] ) , [ holder ( LIBRARY ) ] ) ) ) . toEqual ( [ ] ) ;
1235+ // Resolved, not skipped: a measure the dependency's dataset lacks is still named.
1236+ const typo = moduleBody ( [ LIBRARY ] , { reports : [ report ( { chart : { type : 'bar' , xAxis : 'genre' , yAxis : 'book_total' } } ) ] } ) ;
1237+ const findings = validateChartBindings ( asPackageStack ( typo , [ holder ( LIBRARY ) ] ) ) ;
1238+ expect ( findings . map ( ( f ) => [ f . rule , f . path ] ) ) . toEqual ( [ [ CHART_MEASURE_UNKNOWN , 'reports[0].chart.yAxis' ] ] ) ;
1239+ expect ( findings [ 0 ] . message ) . toContain ( 'dataset "book_metrics"' ) ;
1240+ } ) ;
1241+
1242+ it ( 'resolves a TRANSITIVE dependency’s dataset (the declared closure, ADR-0141)' , ( ) => {
1243+ const middle = { id : 'com.example.middle' , name : 'middle' , dependencies : { [ LIBRARY ] : '^1.0.0' } } ;
1244+ expect ( datasetUnknown ( asPackageStack ( moduleBody ( [ 'com.example.middle' ] ) , [ middle , holder ( LIBRARY ) ] ) ) ) . toEqual ( [ ] ) ;
1245+ } ) ;
1246+
1247+ it ( 'ACCEPTS the same dependency’s dataset on a joined block, a list-view chart and a page chart' , ( ) => {
1248+ const body = moduleBody ( [ LIBRARY ] , {
1249+ reports : [ { name : 'joined_books' , type : 'joined' , blocks : [ { name : 'b' , dataset : 'book_metrics' , rows : [ 'genre' ] , values : [ 'book_count' ] } ] } ] ,
1250+ views : [ { name : 'book_view' , list : { chart : { dataset : 'book_metrics' , dimensions : [ 'genre' ] , values : [ 'book_count' ] } } } ] ,
1251+ pages : [ {
1252+ name : 'book_page' ,
1253+ regions : [ { name : 'main' , components : [ { type : 'object-chart' , properties : { dataset : 'book_metrics' , dimensions : [ 'genre' ] , values : [ 'book_count' ] } } ] } ] ,
1254+ } ] ,
1255+ } ) ;
1256+ expect ( validateChartBindings ( asPackageStack ( body , [ holder ( LIBRARY ) ] ) ) ) . toEqual ( [ ] ) ;
1257+ // CONTROL: the same bindings without the dependency are each refused.
1258+ const undeclared = { ...body , dependencies : undefined } ;
1259+ expect ( datasetUnknown ( asPackageStack ( undeclared , [ holder ( LIBRARY ) ] ) ) . map ( ( f ) => f . path ) ) . toEqual ( [
1260+ 'reports[0].blocks[0].dataset' ,
1261+ 'views[0].list.chart.dataset' ,
1262+ expect . stringMatching ( / ^ p a g e s \[ 0 \] \. .* \. p r o p e r t i e s \. d a t a s e t $ / ) ,
1263+ ] ) ;
1264+ } ) ;
1265+
1266+ it ( 'REFUSES a report over a dataset only an UNDECLARED package holds, naming the holder and the dependency to declare' , ( ) => {
1267+ const findings = datasetUnknown ( asPackageStack ( moduleBody ( undefined ) , [ holder ( LIBRARY ) ] ) ) ;
1268+ expect ( findings ) . toHaveLength ( 1 ) ;
1269+ expect ( findings [ 0 ] ) . toMatchObject ( {
1270+ severity : 'error' ,
1271+ rule : CHART_DATASET_UNKNOWN ,
1272+ where : 'report "books_by_genre"' ,
1273+ path : 'reports[0].dataset' ,
1274+ } ) ;
1275+ expect ( findings [ 0 ] . message ) . toBe (
1276+ `dataset "book_metrics" is held by package "${ LIBRARY } ", outside the declared dependency closure of package ` +
1277+ `"${ MODULE } ", so the installed report's read of it is refused 409 (ADR-0150 D3).` ,
1278+ ) ;
1279+ expect ( findings [ 0 ] . hint ) . toContain ( `add "${ LIBRARY } " to the \`dependencies\` of package "${ MODULE } "` ) ;
1280+ } ) ;
1281+
1282+ it ( 'names the move, never the dependency, when the holder already depends on the reader (a cycle)' , ( ) => {
1283+ const [ finding ] = datasetUnknown ( asPackageStack ( moduleBody ( undefined ) , [ holder ( LIBRARY , { dependencies : { [ MODULE ] : '^1.0.0' } } ) ] ) ) ;
1284+ expect ( finding . severity ) . toBe ( 'error' ) ;
1285+ expect ( finding . hint ) . toContain ( 'dependency cycle' ) ;
1286+ expect ( finding . hint ) . toContain ( 'move this report into that package' ) ;
1287+ expect ( finding . hint ) . not . toContain ( 'Declare' ) ;
1288+ } ) ;
1289+
1290+ it ( 'REFUSES a name two packages of the closure hold, naming both: nothing picks one (red line 2)' , ( ) => {
1291+ const findings = validateChartBindings ( asPackageStack ( moduleBody ( [ LIBRARY , SECOND ] ) , [ holder ( LIBRARY ) , holder ( SECOND ) ] ) ) ;
1292+ // One finding: the report's positions are not judged against either holder's dataset.
1293+ expect ( findings ) . toHaveLength ( 1 ) ;
1294+ expect ( findings [ 0 ] ) . toMatchObject ( { severity : 'error' , rule : CHART_DATASET_UNKNOWN , path : 'reports[0].dataset' } ) ;
1295+ expect ( findings [ 0 ] . message . split ( ': ' ) [ 0 ] ) . toBe (
1296+ `dataset "book_metrics" is held by 2 packages in the declared dependency closure of package "${ MODULE } " ` +
1297+ `("${ LIBRARY } ", "${ SECOND } "), so the installed report's read of it is refused 409` ,
1298+ ) ;
1299+ expect ( findings [ 0 ] . hint ) . toContain ( 'depend on only one of the packages that hold it' ) ;
1300+ } ) ;
1301+
1302+ it ( 'the reading package’s own dataset wins a name its dependency also holds (ADR-0150 D3 step 1)' , ( ) => {
1303+ const own = moduleBody ( [ LIBRARY , SECOND ] , { objects : holder ( MODULE ) . objects , datasets : [ libraryDataset ( ) ] } ) ;
1304+ expect ( validateChartBindings ( asPackageStack ( own , [ holder ( LIBRARY ) , holder ( SECOND ) ] ) ) ) . toEqual ( [ ] ) ;
1305+ } ) ;
1306+
1307+ it ( 'REPORTS, not refuses, when the reader declares a dependency the artifact does not carry; never softens an ambiguity' , ( ) => {
1308+ const findings = datasetUnknown ( asPackageStack ( moduleBody ( [ ELSEWHERE ] ) , [ holder ( LIBRARY ) ] ) ) ;
1309+ expect ( findings ) . toHaveLength ( 1 ) ;
1310+ expect ( findings [ 0 ] . severity ) . toBe ( 'warning' ) ;
1311+ expect ( findings [ 0 ] . message ) . toContain ( `not judged: it may come from "${ ELSEWHERE } ", a dependency this artifact does not carry` ) ;
1312+ expect ( findings [ 0 ] . message ) . not . toContain ( '409' ) ;
1313+ // Two holders in the closure stay an error whatever an outside package holds.
1314+ const ambiguous = datasetUnknown ( asPackageStack ( moduleBody ( [ LIBRARY , SECOND , ELSEWHERE ] ) , [ holder ( LIBRARY ) , holder ( SECOND ) ] ) ) ;
1315+ expect ( ambiguous . map ( ( f ) => f . severity ) ) . toEqual ( [ 'error' ] ) ;
1316+ } ) ;
1317+
1318+ it ( 'a name NO package declares, and any stack judged without the artifact, keep the unchanged sentence' , ( ) => {
1319+ const nowhere = moduleBody ( [ LIBRARY ] , { reports : [ report ( { dataset : 'book_nowhere' } ) ] } ) ;
1320+ const [ finding ] = datasetUnknown ( asPackageStack ( nowhere , [ holder ( LIBRARY ) ] ) ) ;
1321+ expect ( finding . severity ) . toBe ( 'error' ) ;
1322+ expect ( finding . message ) . toBe ( 'binds dataset "book_nowhere", which resolves to no declared dataset — there is no data to render.' ) ;
1323+ expect ( finding . hint ) . toContain ( 'Declared datasets: book_metrics.' ) ;
1324+ // The single-package answer: the body alone, no `packages[]`.
1325+ expect ( datasetUnknown ( moduleBody ( [ LIBRARY ] ) ) ) . toHaveLength ( 1 ) ;
1326+ } ) ;
1327+
1328+ it ( 'the union run reads every body, as the dashboard rule does: a residual top-level `datasets` no longer hides the module’s own' , ( ) => {
1329+ // The `manifest: 'preserve'` composition with a residual top-level `datasets` (the dashboard rule's M2
1330+ // shape): the union keeps that key and folds the bodies' `reports`. BASE (main 47f6c939): the module's
1331+ // report on the module's OWN dataset was refused here, while its per-package pass accepted it.
1332+ const module = defineStack ( {
1333+ manifest : { id : LIBRARY , namespace : 'lib' , name : 'library' , version : '1.0.0' , type : 'module' , dependencies : { [ MODULE ] : '^1.0.0' } } ,
1334+ objects : holder ( LIBRARY ) . objects ,
1335+ datasets : [ { ...libraryDataset ( ) , label : 'Book metrics' } ] ,
1336+ reports : [ { ...report ( ) , label : 'Books by genre' } ] ,
1337+ } as never ) ;
1338+ const app = defineStack ( {
1339+ manifest : { id : MODULE , namespace : 'lib' , name : 'sales' , version : '1.0.0' , type : 'app' } ,
1340+ objects : [ { name : 'lib_account' , fields : { name : { type : 'text' } } } ] ,
1341+ } as never ) ;
1342+ const composed = composeStacks ( [ module , app ] , { manifest : 'preserve' } ) as unknown as Record < string , unknown > & {
1343+ packages : Array < { manifest : Record < string , unknown > } > ;
1344+ } ;
1345+ const residual = { name : 'account_metrics' , object : 'lib_account' , dimensions : [ { name : 'name' , field : 'name' } ] , measures : [ { name : 'n' , aggregate : 'count' } ] } ;
1346+ // `authoringRuleUnionStack` (cli): a top-level key already present is kept, an absent one is folded from the bodies.
1347+ const union = {
1348+ ...composed ,
1349+ datasets : [ residual ] ,
1350+ reports : composed . packages . flatMap ( ( p ) => ( p . manifest . reports as unknown [ ] | undefined ) ?? [ ] ) ,
1351+ } ;
1352+ expect ( union . reports ) . toHaveLength ( 1 ) ;
1353+ expect ( datasetUnknown ( union ) ) . toEqual ( [ ] ) ;
1354+ // CONTROL: the per-package pass of the app, reading the module's dataset, is refused (the module depends on the app).
1355+ const appEntry = composed . packages . find ( ( p ) => p . manifest . id === MODULE ) ! . manifest ;
1356+ const appReads = { ...appEntry , reports : union . reports } ;
1357+ const entries = composed . packages . map ( ( p ) => ( p . manifest . id === MODULE ? { manifest : appReads } : p ) ) ;
1358+ const refused = datasetUnknown ( { ...appReads , manifest : appReads , packages : entries } ) ;
1359+ expect ( refused . map ( ( f ) => f . severity ) ) . toEqual ( [ 'error' ] ) ;
1360+ expect ( refused [ 0 ] . hint ) . toContain ( 'dependency cycle' ) ;
1361+ } ) ;
1362+
1363+ it ( 'reaches the rule table the per-package pass runs; the publish door’s snapshot is unchanged' , ( ) => {
1364+ const declared = asPackageStack ( moduleBody ( [ LIBRARY ] ) , [ holder ( LIBRARY ) ] ) ;
1365+ const undeclared = asPackageStack ( moduleBody ( undefined ) , [ holder ( LIBRARY ) ] ) ;
1366+ const rule = ( stack : Record < string , unknown > ) =>
1367+ runAuthoringRules ( 'validate' , { normalized : stack , parsed : stack } ) . filter ( ( f ) => f . rule === CHART_DATASET_UNKNOWN ) ;
1368+ expect ( rule ( declared ) ) . toEqual ( [ ] ) ;
1369+ expect ( rule ( undeclared ) . map ( ( f ) => f . severity ) ) . toEqual ( [ 'error' ] ) ;
1370+ // The door judges a report write against the live context's datasets, never `packages[]`.
1371+ const gate = runRuntimeAuthoringRules ( { type : 'report' , item : report ( ) , context : { objects : holder ( LIBRARY ) . objects , datasets : [ libraryDataset ( ) ] } } ) ;
1372+ expect ( [ ...gate . errors , ...gate . advisories ] . filter ( ( f ) => f . rule === CHART_DATASET_UNKNOWN ) ) . toEqual ( [ ] ) ;
1373+ } ) ;
1374+ } ) ;
1375+
11791376describe ( 'validateChartBindings — floor' , ( ) => {
11801377 it ( 'is silent with no charts anywhere and tolerates empty input' , ( ) => {
11811378 expect ( validateChartBindings ( baseStack ( ) ) ) . toEqual ( [ ] ) ;
0 commit comments