Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-spp-proposal-eligibility-unrestricted.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@aragon/app": patch
---

Fix proposal creation eligibility showing "Unrestricted" for eligible wallets on restricted SPP processes.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,4 @@ next-env.d.ts
!.agents/shared/**
.playwright-mcp
.patches
.omp
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
import { renderHook } from '@testing-library/react';
import * as useSimulateProposalModule from '@/modules/governance/hooks/useSimulateProposal';
import type { IPermissionCheckGuardResult } from '@/modules/governance/types';
import * as daoService from '@/shared/api/daoService';
import * as useDaoPluginsModule from '@/shared/hooks/useDaoPlugins';
import {
generateDao,
generateDaoPlugin,
generateFilterComponentPlugin,
generateReactQueryResultSuccess,
} from '@/shared/testUtils';
import { pluginRegistryUtils } from '@/shared/utils/pluginRegistryUtils';
import {
generateSppPluginSettings,
generateSppStage,
generateSppStagePlugin,
} from '../../testUtils';
import { useSppPermissionCheckProposalCreation } from './useSppPermissionCheckProposalCreation';

describe('useSppPermissionCheckProposalCreation', () => {
const useDaoPluginsSpy = jest.spyOn(useDaoPluginsModule, 'useDaoPlugins');
const useDaoSpy = jest.spyOn(daoService, 'useDao');
const useSimulateProposalCreationSpy = jest.spyOn(
useSimulateProposalModule,
'useSimulateProposalCreation',
);
const getSlotFunctionSpy = jest.spyOn(
pluginRegistryUtils,
'getSlotFunction',
);

afterEach(() => {
useDaoPluginsSpy.mockReset();
useDaoSpy.mockReset();
useSimulateProposalCreationSpy.mockReset();
getSlotFunctionSpy.mockReset();
});

const createTestParams = (guardResults: IPermissionCheckGuardResult[]) => {
const subPluginMetas = guardResults.map((_result, index) =>
generateDaoPlugin({
address: `0x${String(index + 1).padStart(40, '0')}`,
}),
);

const sppPlugin = generateDaoPlugin({
address: `0x${'a'.repeat(40)}`,
settings: generateSppPluginSettings({
stages: [
generateSppStage({
plugins: subPluginMetas.map((meta) =>
generateSppStagePlugin({ address: meta.address }),
),
}),
],
}),
});

useDaoPluginsSpy.mockReturnValue(
subPluginMetas.map((meta) =>
generateFilterComponentPlugin({ meta }),
),
);
useDaoSpy.mockReturnValue(
generateReactQueryResultSuccess({ data: generateDao() }),
);

const slotFunctions = new Map(
subPluginMetas.map((meta, index) => [
meta.address,
() => guardResults[index],
]),
);
getSlotFunctionSpy.mockImplementation(
() =>
((params: { plugin: { address: string } }) =>
slotFunctions.get(params.plugin.address)?.()) as never,
);

return { daoId: 'dao-test', plugin: sppPlugin };
};

const generateGuardResult = (
result?: Partial<IPermissionCheckGuardResult>,
): IPermissionCheckGuardResult => ({
hasPermission: false,
settings: [],
isLoading: false,
isRestricted: false,
...result,
});

const mockSimulation = (result: {
isLoading: boolean;
isSuccess: boolean;
}) =>
useSimulateProposalCreationSpy.mockReturnValue(
result as ReturnType<
typeof useSimulateProposalModule.useSimulateProposalCreation
>,
);

it('returns isRestricted true for a restricted process even when the connected wallet can create a proposal', () => {
const settings = [[{ term: 'Members', definition: 'Listed only' }]];
const guardResult = generateGuardResult({
isRestricted: true,
settings,
hasPermission: true,
});
const params = createTestParams([guardResult]);
mockSimulation({ isLoading: false, isSuccess: true });

const { result } = renderHook(() =>
useSppPermissionCheckProposalCreation(
params as Parameters<
typeof useSppPermissionCheckProposalCreation
>[0],
),
);

expect(result.current.isRestricted).toBeTruthy();
expect(result.current.settings).toEqual(settings);
});

it('returns isRestricted false when no sub-plugin restricts proposal creation', () => {
const params = createTestParams([
generateGuardResult({ isRestricted: false }),
generateGuardResult({ isRestricted: false }),
]);
mockSimulation({ isLoading: false, isSuccess: true });

const { result } = renderHook(() =>
useSppPermissionCheckProposalCreation(
params as Parameters<
typeof useSppPermissionCheckProposalCreation
>[0],
),
);

expect(result.current.isRestricted).toBeFalsy();
});

it('returns isRestricted true when any sub-plugin restricts proposal creation', () => {
const restrictedSettings = [
[{ term: 'Voting power', definition: '≥10' }],
];
const params = createTestParams([
generateGuardResult({ isRestricted: false }),
generateGuardResult({
isRestricted: true,
settings: restrictedSettings,
}),
]);
mockSimulation({ isLoading: false, isSuccess: true });

const { result } = renderHook(() =>
useSppPermissionCheckProposalCreation(
params as Parameters<
typeof useSppPermissionCheckProposalCreation
>[0],
),
);

expect(result.current.isRestricted).toBeTruthy();
expect(result.current.settings).toEqual(restrictedSettings);
});

it('derives hasPermission from the proposal creation simulation', () => {
const params = createTestParams([
generateGuardResult({ isRestricted: true }),
]);
mockSimulation({ isLoading: false, isSuccess: false });

const { result } = renderHook(() =>
useSppPermissionCheckProposalCreation(
params as Parameters<
typeof useSppPermissionCheckProposalCreation
>[0],
),
);

expect(result.current.hasPermission).toBeFalsy();
expect(result.current.isRestricted).toBeTruthy();
});

it('returns isLoading true while the simulation is loading', () => {
const params = createTestParams([generateGuardResult()]);
mockSimulation({ isLoading: true, isSuccess: false });

const { result } = renderHook(() =>
useSppPermissionCheckProposalCreation(
params as Parameters<
typeof useSppPermissionCheckProposalCreation
>[0],
),
);

expect(result.current.isLoading).toBeTruthy();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,16 @@ export const useSppPermissionCheckProposalCreation = (
result?.isRestricted ? result.settings : [],
);

// The simulation only checks the connected wallet's permission; whether the process itself is restricted comes
// from the sub-plugin creation rules.
const isRestricted = pluginProposalCreationGuardResults.some(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we probably could've used settings.length as a proxy for isRestricted too, but I think your version reads better 👍

(result) => result?.isRestricted === true,
);

return {
hasPermission: permissionGranted,
settings,
isLoading,
isRestricted: !permissionGranted,
isRestricted,
};
};
Loading